Your Ad Here

Wednesday, February 27, 2008

Shuttle Sort - Simple Insertion Sort

#include< stdio.h>
#include< conio.h>

void shutsort(int a[],int n)
{
int j,i=1,mid;

while(i< n)
{
j=i-1;
while(j>=0)
{
if(a[j]>a[j+1])
{
mid = a[j];
a[j] = a[j+1];
a[j+1]=mid;
j--;
}
else
break;
}
i++;
}
}

main()
{
int a[10],i,n;
clrscr();

printf("Enter The number Of Elements\t: ");
scanf("%d",&n);
for(i=0;i< n;i++)
{
printf("\nElement %d\t: ",i+1);
scanf("%d",&a[i]);
}

printf("\nArray Befor Sorting : ");
for(i=0;i< n;i++)
printf("%5d",a[i]);
shutsort(a,n);

printf("\nArray After Sorting : ");
for(i=0;i< n;i++)
printf("%5d",a[i]);
getch();
return 0;
}

/* OUTPUT
Enter The number Of Elements : 5

Element 1 : 21

Element 2 : 36

Element 3 : 54

Element 4 : 98

Element 5 : 1

Array Befor Sorting : 21 36 54 98 1
Array After Sorting : 1 21 36 54 98
*/

No comments:

Your Ad Here