In this tutorial you will learn about the Insertion Sort Algorithm and its application with practical example.
Insertion Sort Program In C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#include<stdio.h> int main(){ int i, j, n, temp, nums[30]; printf("Enter number of elements: "); scanf("%d",&n); printf("Enter %d integer numbers\n", n); // Loop to input the elements for(i=0;i<n;i++) scanf("%d",&nums[i]); // Implementation of insertion sort algorithm for(i=1;i<n;i++){ temp=nums[i]; j=i-1; while((temp<nums[j])&&(j>=0)){ nums[j+1]=nums[j]; j=j-1; } nums[j+1]=temp; } printf("Elements after sorting: "); for(i=0;i<n;i++) printf(" %d",nums[i]); return 0; } |
Output:-
sdf