Following C program is used to find the insertion sort mechanism.
#include<stdio.h> void InsertionSort(int a[], int n) { int j, p; int tmp; for(p = 1; p < n; p++) { tmp = a[p]; for(j = p; j > 0 && a[j-1] > tmp; j--) a[j] = a[j-1]; a[j] = tmp; } } int main() { int i, n, a[10]; printf("Enter the number of elements :: "); scanf("%d",&n); printf("Enter the elements :: "); for(i = 0; i < n; i++) { scanf("%d",&a[i]); } InsertionSort(a,n); printf("The sorted elements are :: "); for(i = 0; i < n; i++) printf("%d ",a[i]); printf("\n"); return 0; }
OUTPUT:
Enter the number of elements :: 7 Enter the elements :: 10 30 80 20 60 40 50 The sorted elements are :: 10 20 30 40 50 60 80