Thursday , December 8 2016
Home / C / C program for Quick Sort

C program for Quick Sort

C program which demonstrate the sorting technique using quick sort technique
 
//This module is used to select and place the pivot element at its location in quick sort algo
int partition(int a[], int p,int r)
{
int x,i,j,temp;
x=a[r];
i=p-1;
for(j=p;j<=r-1;j++)
{
if(a[j]<=x)
{i=i+1;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}}
temp=a[i+1];
a[i+1]=a[r];
a[r]=temp;
return i+1;
}
void quicksort(int a[],int p,int r) //This module is use to do partition in array for quick sort
{
int q;
if(p<r)
{
q=partition(a,p,r);
quicksort(a,p,q-1);
quicksort(a,q+1,r);
}
}
void main()
{
int a[50], i,j,k,n;
clrscr();
printf(“Enter total number of elements : “);
scanf(“%d”,&n);
n=n;
printf(“\n Enter elements”);
for(i=1;i<=n;i++)
{
scanf(“%d”,&a[i]);
}
quicksort(a,1,n);
printf(“Elements after sorting is \n”);
for(i=1;i<=n;i++)
{
printf(“\t%d”,a[i]);
}
getch();
}
 

OUTPUT

 
tags: Sorting technique, quick sort techniqe, best algorithm for sorting, program in c for quick sort 
Also See:   C program for Lamport Logical Clock

Check Also

Hide Mouse Pointer C Program

How to Hide a Mouse Pointer Program in C : This program will show and hide ...

One comment

  1. Good post. But a little mistake there in if condition.
    if(p in void quicksort()// if(p<r)

    Also please remove clrscr(); as it is not supported by most of the compiler and in linux.
    Visit here : Quick sort program in c
    Here quick sort is explained pretty well using the same method as above.

Leave a Reply

Your email address will not be published. Required fields are marked *

Preventing spam * Time limit is exhausted. Please reload the CAPTCHA.