C Programming

How to Use Selection Sort in C Language

Sorting is an important concept to arrange the data in sequence. It is used to determine the data and make it visualize. There are different sorting techniques used in the C programming language and selection sort is one of them.

If you don’t know what selection sort is and how to use it in C programming language, follow this guide for further details.

Selection Sort in C Language

In C, selection sort is a dependable and speedy sort algorithm built on comparison processes. One element is added after another during the iteration process. To move the leading element to the top of the array, the lowest element in the array must be picked and swapped. The selection sort does compare all the elements one-by-one in the array and sorts them in ascending or descending order.

Algorithm of Selection-Sort 

The selection sort algorithm works in the following way:

  • Set the first location of the array as Min-element
  • Search the lowest element in the array and swap it with the first location
  • Set the second location as second Min-element
  • Swap with the second lowest element in the array
  • Repeat this process till the last index is sorted.

How Selection Sort Works

We first take an array and choose what to decide. Whether you are going with sorting the elements in ascending order or descending according to the requirements.

Let’s take an unsorted array and sort it in ascending order.

22 4 8 30 7

First-Iteration

In the above array we have 5 indexes starting from 0 to 4.

Let’s take an unsorted array of 5-elements A[22,4,8,30,7]. Now apply selection-sort in ascending order:

Put 0 index as minimum digit and find the minimum digit in the array by comparing the 0-index element to the all as 4 is minimum so it replaces with 22 and the array becomes:

4 22 8 30 7

Second-Iteration

Now select index1 as the second minimum index and compare its element with all over the array as 7 is the second lowest element so it will be replaced with the digit 22.

4 7 8 30 22

Third-Iteration

Now select the second index of the array as the third lowest element and find it by comparing all remaining unsorted elements as index-2 element is 8 remaining are 30, 22 which is greater than 8 so it cannot move where it is already sorted as shown below.

4 7 8 30 22

Fourth-Iteration

As moving towards the third index, mark it as the 4th smallest digit and compare them with the fifth element digit of the array as 30 is greater than 22 so 22 will swap with 30 so the array will become:

4 7 8 22 30

Fifth-Iteration

As in the fifth iteration, it compares the last index which is 4 to all array index elements and here its element is already sorted and the array is sorted completely with the help of selection sort.

4 7 8 22 30

Implementation of Selection Sort in C

Follow the below-mentioned code to perform selection sort in C programming language.

#include <stdio.h>
int main()
{
    int b[50], digit, n, m, extra;
    printf("\n Now Enter the total # of Digits  :  ");
    scanf("%d", &digit);
    printf("\n Now insert the elements of the Array  :  ");
    for(n = 0; n < digit; n++)
        scanf("%d", &b[n]);

    for(n = 0; n < digit; n++) {
        for(m = n + 1; m < digit; m++) {
            if(b[n] > b[m]) {
                extra = b[n];
                b[n] = b[m];
                b[m] = extra;
            }
        }
    }
    printf("\n Result of Selection-Sort is : ");
    for(n = 0; n < digit; n++)  {
        printf(" %d \t", b[n]);
    }
    printf("\n");
    return 0;
}

In the above program, the first user enters the total number of elements that need to be sorted, then one after the other user inserts the digits and hence with the help of a nested-for loop, sort these digits in ascending order as you see in the below output.

Output

Conclusion 

Selection sort is one of the simple sorting algorithms that sorts the array elements in the C programming language. It uses the simple for loop to search for the lowest elements in an array and sort them accordingly. It provides the best optimal solution to arrange the small arrays in a C programming language. You can find its step-by-step implementation in the above-mentioned guidelines.

About the author

Kaynat Asif

My passion to research new technologies has brought me here to write for the LinuxHint. My major focus is to write in C, C++, and other Computer Science related fields. My aim is to share my knowledge with other people.