Program to implement Quick Sort!

#include<stdio.h>
#define MAX 50

int qsort(int a[], int from, int to)
{
int pivot, lvalues[MAX], partition;
int i, j;

if(from>=to) return 0;

printf("In the qsort function with from=%d and to=%d\n",from,to);
for(i=from; i<=to; i++) printf("%d ",a[i]);
printf("\n");
pivot=a[to];
j=0; partition=0;
for(i=from;i<=to;i++)
{
if(a[i]>pivot) lvalues[j++]=a[i];
else a[partition++]=a[i];
}
for(i=0;i<j;i++) a[partition+i]=lvalues[i];

printf("%d\n",partition);

qsort(a,from,partition-2);
qsort(a,partition,to);

return partition;
}

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

printf("Enter the number of values: ");
scanf("%d",&n);
printf("Enter the elements of the array: ");
for(i=0;i<n;i++) scanf("%d",&a[i]);

qsort(a,0,n-1);

for(i=0;i<n;i++) printf("%d ",a[i]);
printf("\n");

return 0;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s