/* Quick Sort */
#include<stdio.h>
#include<conio.h>
int split(int*,int,int);
void main()
{
int arr[10]={ 10, 1, 9, 11,46, 20, 15, 0, 72, 2};
int i;
void quicksort(int *,int,int);
clrscr();
printf(" Quick Sort. \n");
printf("\n Array before sorting : \n");
for(i=0;i<=9;i++)
printf("%d\t",arr[i]);
quicksort(arr,0,9);
printf("\n Array after sorting :\n");
for(i=0;i<=9;i++)
printf("%d\t",arr[i]);
getch();
}
void quicksort(int z[],int lower,int upper)
{
int i;
if(upper>lower)
{
i=split(z,lower,upper);
quicksort(z,lower,i-1);
quicksort(z,i+1,upper);
}
}
int split(int z[],int lower,int upper)
{
int i,a,b,t;
a=lower+1;
b=upper;
i=z[lower];
while(b>=a)
{
while(z[a]<i)
a++;
while(z[b]>i)
b--;
if(b>a)
{
t=z[a];
z[a]=z[b];
z[b]=t;
}
}
t=z[lower];
z[lower]=z[b];
z[b]=t;
return b;
}
No comments:
Post a Comment