C Programming

Insertion Sort

Insertion sort is a simple sorting algorithm, a comparison sort in which the sorted array (or list) is built one entry at a time. Every iteration of insertion sort removes an element from the input data, inserting it into the correct position in the already-sorted list, until no input elements remain.

Language : C

[sourcecode language='c']
//INSERTION SORTING
#include
#include
void main()
{
int i,j,a[50],n,temp;
char ch;
clrscr();
do{
printf(“Enter the number of elements to be inserted in array (less then 50)”);
scanf(“%d”,&n);
printf(“\nEnter the elements of array \n”);
for(i=0;i scanf("%d",&a[i]);

for(i=1;i<=n-1;i++)
{
temp=a[i];
j=i-1;
while((temp=0))
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;

}

printf(“\n The sorted array is:”);
for(i=0;i printf(“\t %d”,a[i]);

fflush(stdin);
printf(“\n Do you want to continue y/n”);
scanf(“%c”,&amp;ch);
}while(ch==’y');
}
[/sourcecode]



Leave a Comment

Notify me of followup comments via e-mail. You can also subscribe without commenting.


WordPress - Vaibhav Kanwal