# quick sort in c

Make a list of items that need to be sorted, lets apply in an array. Choose any element as pivot element from the array list.(Complexity largely depends on choosing the pivot element).Rearrange the array list so that all the elements with value less than the pivot will come before the pivot and the element with value greater will come after the pivot with in the same array, which make pivot element in the sorted position.(If the reverse the order we are reversing the sorting order that is descending). Apply recursively the 3rd step to the sub array of the element with smaller values and separate the sub array of the elements with the greater values.

PROGRAM:

#include<stdio.h>

void quicksort(int ,int,int);

int main(){

int x,size,i;

printf(“Enter size of the array: “);

scanf(“%d”,&size);

printf(“Enter %d elements: “,size);

for(i=0;i<size;i++)

scanf(“%d”,&x[i]);

quicksort(x,0,size-1);

printf(“Sorted elements: “);

for(i=0;i<size;i++)

printf(” %d”,x[i]);

return 0;

}

void quicksort(int x,int first,int last){

int pivot,j,temp,i;

if(first<last){

pivot=first;

i=first;

j=last;

while(i<j){

while(x[i]<=x[pivot]&&i<last)

i++;

while(x[j]>x[pivot])

j–;

if(i<j){

temp=x[i];

x[i]=x[j];

x[j]=temp;

}

}

temp=x[pivot];

x[pivot]=x[j];

x[j]=temp;

quicksort(x,first,j-1);

quicksort(x,j+1,last);

}

}

OUTPUT:

Enter size of the array: 5

Enter 5 elements: 3 8 0 1 2

Sorted elements: 0 1 2 3 8