C Programming Code Examples C > Sorting Searching Code Examples C Program - Insertion Sort implementation C Program - Insertion Sort implementation Insertion sort algorithm picks elements one by one and places it to the right position where it belongs in the sorted list of elements. In the following C program we have implemented the same logic. Before going through the program, lets see the steps of insertion sort with the help of an example. Input elements: 96 17 8 12 0 Step 1: 96 17 8 12 0 (the bold elements are sorted list and non-bold unsorted list) Step 2: 17 96 8 12 0 (each element will be removed from unsorted list and placed at the right position in the sorted list) Step 3: 8 17 96 12 0 Step 4: 8 12 17 96 0 Step 5: 0 8 12 17 96 C Program - Insertion Sort implementation #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); // This loop would store the input numbers in array for(x=0;x<count;x++) scanf("%d",&number[x]); // Implementation of insertion sort algorithm for(x=1;x<count;x++){ temp=number[x]; j=x-1; while((temp<number[j])&&(j>=0)){ number[j+1]=number[j]; j=j-1; } number[j+1]=temp; } printf("Order of Sorted elements: "); for(x=0;x<count;x++) printf(" %d",number[x]); return 0; }