The selection sort program in c is techniques to sort the elements into a particular order.

Selection sort technique
In this technique, we compare the first element within the number with the second number if it is small then we save these numbers otherwise not. Then again the first element is compared with the 3rd element if it is less we swap first element and third element. Again we test the first element with the fourth element and so on. This is pass 1 for sorting after this past we will get the smallest number at the top. In this past 2, we compare a second element with the third element, if it is less we perform sorting else not. Then the second compares with fourth and so on. This process continues until we get a sorted list.

Selection sort algorithm

  1. Take the size of an array. N
  2. Take numbers input from the user. a[N]
  3. Traverse the array till end
  4. Compare i array index value with i+1 and bubble up large value to end
  5. Print sorted array

Selection sort program in c

#include <stdio.h>
int main()
int array[100], n, c, d, position, swap;

printf("Enter number of elements\n");
scanf("%d", &n); //Enter range of array

printf("Enter %d integers\n", n);

for ( c = 0 ; c < n ; c++ )
scanf("%d", &array[c]); // Enter values in array

for ( c = 0 ; c < ( n - 1 ) ; c++ ) //code for selection sort
position = c;

for ( d = c + 1 ; d < n ; d++ )
if ( array[position] > array[d] )
position = d;
if ( position != c )
swap = array[c];
array[c] = array[position];
array[position] = swap;
printf("Sorted list in ascending order:\n");

for ( c = 0 ; c < n ; c++ )
printf("%d\n", array[c]);

return 0;

Output :

Selection sort program in c

Write A Comment