Quick Sort

#include<stdio.h>

void quicksort(int a[],int l,int h)
{ 
    int p;
    if(l<h)
    {	
        p=partition(a,l,h);
        quicksort(a,l,p-1);
        quicksort(a,p+1,h);
    }
}

void swap(int a[],int i,int j)
{
 int temp;
 temp=a[i];
 a[i]=a[j];
 a[j]=temp;
}

int partition(int a[],int i,int j)
{
    int u,v;
    u=i;
    while(i<j)
    {
        while(a[i]<=a[u])
            i++;
        while(a[j]>a[u])
            j--;
        if(i<j)
            swap(a,i,j);
    }
    swap(a,j,u);
    return j;
}

void main()
{
    int a[100],i,n;
    printf("Enter the number of elements in the array ");
    scanf("%d",&n);
    printf("Enter array elements ");
    for (i=0;i<n;i++)
        scanf("%d",&a[i]);
	quicksort(a,0,n-1);
	printf("The sorted array is ");
	for (i=0;i<n;i++)
        printf("%d ",a[i]);
}

/*Output
Enter the number of elements in the array 5
Enter array elements 16 98 66 34 11
The sorted array is 11 16 34 66 98
*/

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