QUICK SORT Program in C

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

hackingzo ne s.

co m

http://hackingzo nes.co m/?p=1599

QUICK SORT program in C


Quick sort Programming Source code it has the header f ile quick.h it contains void swap(int *x,int *y),void display(int A[],int n),void insertionsort(int A[],int n),int median(int A[],int lef t,int right),void quicksort(int A[],int lef t,int right) by using these f unction and there variable we can use in the main f unction of the program Quick.h f ile

#include #include #def ine cutof f (3) void swap(int *x,int *y) { int temp; temp=*x; *x=*y; *y=temp;} void display(int A[],int n) { int i; f or(i=0;i printf (%d\t,A[i]); } void insertionsort(int A[],int n) { int j,p,temp; f or(p=1;p { temp=A[p]; f or(j=p;j>0&&A[j-1]>temp;j) A[j]=A[j-1]; A[j]=temp; } }

int median(int A[],int lef t,int right) { int center=(lef t+right)/2; if (A[lef t]>A[center]) swap(&A[lef t],&A[center]); if (A[lef t]>A[right]) swap(&A[lef t],&A[right]); if (A[center]>A[right]) swap(&A[center],&A[right]); if (A[lef t]<=A[center]<=A[right]) swap(&A[center],&A[right-1]); return A[right-1]; } void quicksort(int A[],int lef t,int right) { int i,j,pivot; if (lef t+cutof f <=right) { pivot=median(A,lef t,right); i=lef t; j=right-1; f or(;;) { while(A[++i] {} while(A[--j]>pivot) {} if (i swap(&A[i],&A[j]); else break; } swap(&A[i],&A[right-1]); quicksort(A,lef t,i-1); quicksort(A,i+1,right); } else insertionsort(A+lef t,right-lef t+1); } Quick.c f ile QUICK SORT #includequick.h void main() { int A[20],n,i; clrscr(); printf (\n how many numbers do u want to sort:\t); scanf (%d,&n); printf (\n\n Enter the values); f or(i=0;i { printf (\n A[%d]\t,i); scanf (%d,&A[i]); }

quicksort(A,0,n-1); display(A,n); getch(); } OUT PUT QUICK SORT How many numbers do u want to sort: 5 Enter the values A[0] 25 A[1] 10 A[2] 89 A[3] 65 A[4] 15 10 15 25 65 89

Relat ed Post
Postf ix Program in c Heap Sort program in c Enter your email address:

Delivered by FeedBurner

Post Footer automatically generated by Add Post Footer Plugin f or wordpress.

You might also like