C Programming Code Examples C > Sorting Searching Code Examples Selection Sort Program in C Selection Sort Program in C In Selection sort, the smallest element is exchanged with the first element of the unsorted list of elements (the exchanged element takes the place where smallest element is initially placed). Then the second smallest element is exchanged with the second element of the unsorted list of elements and so on until all the elements are sorted. In the following C program we have implemented the same logic. Before going through the program, lets see the steps of selection sort with the help of an example: Entered elements: 23 0 -90 88 17 Step 1: -90 0 23 88 17 (23 and -90 exchanged position) Step 2: -90 0 23 88 17 (0 is at right place, no exchange needed) Step 3: -90 0 17 88 23 (23 and 17 exchanged position) Step 4: -90 0 17 23 88 (88 and 23 exchanged position) C Program - Selection sort #include<stdio.h> int main(){ /* Here x & j for loop counters, temp for swapping, count for total number of elements, number[] to store the input numbers in array. You can increase or decrease the size of number array as per requirement */ int x, j, count, temp, number[25]; printf("How many numbers u are going to enter?: "); scanf("%d",&count); printf("Enter %d elements: ", count); // Loop to get the elements stored in array for(x=0;x<count;x++) scanf("%d",&number[x]); // Logic of selection sort algorithm for(x=0;x<count;x++){ for(j=x+1;j<count;j++){ if(number[x]>number[j]){ temp=number[x]; number[x]=number[j]; number[j]=temp; } } } printf("Sorted elements: "); for(x=0;x<count;x++) printf(" %d",number[x]); return 0; }