Skip to content

Instantly share code, notes, and snippets.

@swarupsro
Last active October 4, 2016 19:24
Show Gist options
  • Save swarupsro/76dc6623b871553cfddb359e9b5ca068 to your computer and use it in GitHub Desktop.
Save swarupsro/76dc6623b871553cfddb359e9b5ca068 to your computer and use it in GitHub Desktop.
Insertion Sort
#include <stdio.h>
int main()
{
int A[20], n, k, i, loc, temp;
A[0] = -32768;
printf("Enter the number of elements: ");
scanf("%d",&n);
printf("\n Input %d Integers: ", n);
for(i=1; i<=n; i++)
scanf("%d", &A[i]);
for(k=2; k<=n; k++)
{
temp = A[k];
loc = k-1;
while(temp < A[loc])
{
A[loc+1] = A[loc];
loc--;
}
A[loc+1] = temp;
}
printf("\n\nSorted elements are : \n");
for(i=1; i<=n; i++)
printf("%d\t",A[i]);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment