Tweet
/* Recursive Quick Sort in C */
#include<stdio.h>
int n;
void swap(int *a,int x,int y)
{
int temp=*(a+x);
*(a+x)=*(a+y);
*(a+y)=temp;
}
void quicksort(int *a,int l,int h)
{
int x;
if(l>=h)
return;
x=partition(a,l,h);
quicksort(a,l,x-1);
quicksort(a,x+1,h);
}
int partition(int *a,int l,int h)
{
int i,j,pv;
pv=a[l];
i=l,j=h;
while(i<=j)
{
while(a[i]<=pv)
i++;
while(a[j]>pv)
j--;
if(i<j)
swap(a,i,j);
}
swap(a,l,j);
int k=0;
printf("\n");
for(k=0;k<n;k++)
printf("%4d",a[k]);
printf("\n");
return(i-1);
}
main()
{
int i,j;
printf("\nProgram to sort data using quick sort:-\n\n");
printf("Enter number of elements:");
scanf("%d",&n);
int a[n];
srand(time(NULL));
for(i=0;i<n;i++)
a[i]=rand()%100;
printf("\nBefore Sorting:-\n\n");
for(i=0;i<n;i++)
printf("%4d",a[i]);
quicksort(a,0,n-1);
printf("\n\nAfter Sorting:-\n\n");
for(i=0;i<n;i++)
printf("%4d",a[i]);
printf("\n");
}
Recursive Quick sort in C programming
Posted by
LAHAUL SETH
~
Recursive Quick sort in C programming
2011-11-29T10:56:00+05:30
LAHAUL SETH
Programming in C
|
Sorting
|
Comments
Recursive Quick sort in C programming
2011-11-29T10:56:00+05:30
LAHAUL SETH
Programming in C
|
Sorting
|