Program
Time Complexity: O(n2)
#include <stdio.h> int main() { printf("***************************************************************"); printf("\n***************************************************************"); printf("\n** WAP to sort the elements of an array using insertion sort **"); printf("\n** Created by Sheetal Garg **"); printf("\n** Assistant Professor **"); printf("\n** Phone No:9467863365 **"); printf("\n***************************************************************"); printf("\n***************************************************************\n"); int a[10],n,i,j,key; printf("enter the no. of elements in array"); scanf("%d",&n); printf("enter array elements"); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=1;i<n;i++) { key=a[i]; j=i-1; while(j>=0 && key<a[j]) { a[j+1]=a[j]; j=j-1; } a[j+1]=key; } printf("array after sorting is \n"); for(i=0;i<n;i++) printf("%d \t",a[i]); return 0; }
Output
***************************************************************** ***************************************************************** ** WAP to sort the elements of an array using insertion sort ** ** Created by Sheetal Garg ** ** Assistant Professor ** ** Phone No:9467863365 ** ***************************************************************** ***************************************************************** enter the no. of elements in array 6 enter array elements12 32 21 10 9 8 array after sorting is 8 9 10 12 21 32