Saturday, August 6, 2011

WAP to show Insertion Sort

//WAP to show Insertion Sort
#include<stdio.h>
#include<conio.h>
main()
{ int i,j,a[30],temp,last;
clrscr();
printf("\n\nEnter no. of elements:");
scanf("%d",&last);

printf("\n\nEnter %d elements:",last);
for(i=0 ; i<last ; i++)
{ scanf("%d",&a[i]);
}
printf("\n\nEntered elements are:");
for(i=0 ; i<last ; i++)
{ printf("%d",a[i]);
}

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


printf("\n\nElements of array after aorting is:");

for(i=0 ; i<last ; i++)
{ printf("%d",a[i]);
}
getch();
}

No comments:

Post a Comment

Programming the Whole World!