Selection Sort in C: How to implement Selection Sort in C

Selection Sort in C: How to implement Selection Sort in C

Selection Sort in C

Selection sort is a classic sorting Algorithm in Computer Science. It can be used to say like sort an array of integers in C from the Smallest integer to the Largest integer or Vice-versa. The way the Selection Sort algorithm works are considered a bit simpler compared to some Advanced sorting algorithms.

In this article, you're going to learn how to implement the Selection Sort algorithm in C in a much simpler way.

Let's get started

First, We basically need the unsorted array for example:

int a[] = {20,2,4,1,3,7,5,6,9,8,0};

So, what we do, is have an unsorted portion of the array on the right-hand side and the sorted portion of the array on the left-hand side. One index at a time, we continually identify the smallest element in that remaining unsorted portion of the array and swap it with the element at that current index. For example, when the algorithm first starts, the entire array is considered to be unsorted just like this:

// initial array: 20,2,4,1,3,7,5,6,9,8,0

So, what we want to do is we need to look at the first index, then find the smallest in the right-handed side unsorted array and swap it with the element at that index. in the above case, "0" is the smallest element in that array and it needs to be swapped with the element with the current position you are looking at, in that case, which is "20".

So, here is the list of the pseudocode of how the algorithm function, later on, there will be the implementation of the actual code.

The pseudocode of how the algorithm works is like this:

// initial array: 20,2,4,1,3,7,5,6,9,8,0
//         step 0: 0,2,4,1,3,7,5,6,9,8,20
//         step 1: 0,1,4,2,3,7,5,6,9,8,20
//         step 2: 0,1,2,4,3,7,5,6,9,8,20
//         step 3: 0,1,2,3,4,7,5,6,9,8,20
//         step 4: 0,1,2,3,4,5,7,6,9,8,20
//         step 5: 0,1,2,3,4,5,6,7,9,8,20
//         step 6: 0,1,2,3,4,5,6,7,9,8,20
//         step 7: 0,1,2,3,4,5,6,7,8,9,20

Alright!! Since you've seen how the algorithm works in pseudocode, let's go ahead and implement the algorithm in C just like this:

#include<stdio.h>

void main(){
    int a[] = {20,2,4,1,3,7,5,6,9,8,0};
    int i,j,length;
    length = 11;

    for (i = 0; i < length; i++){ // This loop is going through the array one at a time, assigning each element with i.
      for(j = i + 1; j < length; j++){ // This inner loop is going to find the index of the minimum number in unsorted portion of the array.
       int Lowest_pos = i; // starting with the assumption that the minimum number is located at the index we are looking right now which is: "i"
       if (a[j] < a[i]) lowest_pos = j; // we need to update the "lowest position (lowest_pos)" whenever we find the number that is smaller than the current number.
       if (lowest_pos != i){ // We are doing the swap where necessary
         int temp = a[i]; // Temp Variable will hold the value of a[i] before being overrode
         a[i] = a[lowest_pos];
         a[lowest_pos] = temp;
         }
       }
     }
      for (i = 0; i < length; i++)
           printf("a[%d] = %d\n", i, a[i]);
}

Here is the output result of the algorithm :

image.png

Conclusion

Now you can implement the Selection Sort algorithm in C, if you still don't get what's going on; try to look at the code, break it down piece by piece (also check out the comments) and after a couple of minutes you gonna understand actually what's going on. Happy coding!!