You are on page 1of 2

C program for Quick Sort

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

int *a,n,i,j;

void quicksort();

int partition();

void main(){

clrscr();

printf("Enter the number of element\n");

scanf("%d",&n);

a = (int*)calloc(n*sizeof(int));

printf("Enter the elements\n");

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

scanf("%d",&a[i]);

printf("Elements before sorting are : ");

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

printf("%d ",a[i]);

quicksort(0,n-1);

printf("Elements after sorting are : ");

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

printf("%d ",a[i]);

getch();

}
C program for Quick Sort

void quicksort(int low,int high){

if(low<high){

j=partition(low,high);

quicksort(low,j-1);

quicksort(j+1,high);

} }

int partition(int low,int high){

int temp,pivot;

pivot=low;

i=low;

j=high;

while(i<j){

while((i<high)&&(a[i]<=a[pivot])){

i=i+1;

while(a[j]>a[pivot]){

j=j-1;

if(i<j){

temp=a[i];

a[i]=a[j];

a[j]=temp;

temp=a[pivot];

a[pivot]=a[j];

a[j]=temp;

return j;

You might also like