Skip to content

Instantly share code, notes, and snippets.

@indrajithi
Last active August 29, 2015 14:05
Show Gist options
  • Select an option

  • Save indrajithi/9ddd78d36d6daa67e09b to your computer and use it in GitHub Desktop.

Select an option

Save indrajithi/9ddd78d36d6daa67e09b to your computer and use it in GitHub Desktop.
INSERTION SORT IN C
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
main()
{
int a[100],i,j,key;
srand(time(NULL));
for(i=1; i<=10; i++)
a[i]=rand()%100;
for(i=1; i<=10; i++)
printf("%d , ",a[i]);
printf("\n----------------INSERTION_SORT-----------------\n");
for (j = 2; j <=10; ++j)
{
key=a[j];
i=j-1;
while(i>0 && a[i]>key)
{
a[i+1]=a[i];
i=i-1;
}
a[i+1]=key;
}
for(i=1; i<=10; i++)
printf("%d , ",a[i]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment