// Quick Sorting
#include<stdio.h>
#include<conio.h>
int main()
{
int i,k[5];
printf("Enter 10 numbers one by one ");
for(i=0;i<5;i++)
scanf("%d",&k[i]);
printf("The original numbers are \n");
for(i=0;i<5;i++)
printf("%5d",k[i]);
quick_sort(k,0,4);
printf("\nNumbers are in sorted order\n");
for(i=0;i<5;i++)
{
printf("%d",k[i]);
}
getch();
return 0;
}
quick_sort(int k[5],int first,int last)
{
int pivot,i,j,temp;
if(first<last)
{
pivot=first; //5
i=first; //3
j=last; //7
while(i<j) //3<7
{
while(k[i]<=k[pivot]&&i<last) //3<=5&&3<7
i++; //
while(k[j]>k[pivot]) //7>5
j--; //
if(i<j)
{
temp=k[i];
k[i]=k[j];
k[j]=temp;
}
}
temp=k[pivot];
k[pivot]=k[j];
k[j]=temp;
quick_sort(k,first,j-1);
quick_sort(k,j+1,last);
}
return 0;
}
Tags
MCA