Ask your own question, for FREE!
Computer Science 16 Online
OpenStudy (anonymous):

c-programming quick sort question why is the value of left and right changing ?

OpenStudy (anonymous):

OpenStudy (anonymous):

i made it but am stuck in the dry tun :/ help anyone ?

OpenStudy (anonymous):

-run

OpenStudy (bibby):

for sanity's sake ``` #include<stdio.h> #include<conio.h> void quicksort(int [],int,int); void main() { int value[50],r,a; clrscr(); printf("\n ENTER RANGE OF YOUR LIST = "); scanf("%d",&r); printf("\n \t \t ENTER %d VALUES ",r); for(a=0;a<r;a++) { printf("ENTER %d VALUE = " ,a+1); scanf("%d",&value[a]); } quicksort(value,0,r-1); printf("\n \t \t THE SORTED SERIES IS : "); for(a=0;a<r;a++) printf(" %d ",value[a]); getch(); } void quicksort(int value[],int left,int right) { int i=left,j=right,pivot=value[left],swap; while(i<j) { while(value[i]<pivot) i++; while(value[j]>pivot) j--; if(i<=j) { swap=value[i]; value[i]=value[j]; value[j]=swap; i++; j--; } } if(left<j) quicksort(value,left,j); if(right>i) quicksort(value,i,right); } ```

OpenStudy (anonymous):

sorry bout that ....... can u tell me why are the left and right variable values changing ? the dry run is not as i expected

OpenStudy (bibby):

you might want to use a helper function to partition, I'm comparing your code to some working code right now

OpenStudy (anonymous):

i know aboutt the partition function but i can't get it ....... that's why i made this (without partition)

OpenStudy (bibby):

I'm too braindead to step through your code right now. Examine this http://www.comp.dit.ie/rlawlor/Alg_DS/sorting/quickSort.c

OpenStudy (anonymous):

ahhh D: the problem is i can never understand other's code so i make up my own logic :/

OpenStudy (anonymous):

and now i am stuck ....................

OpenStudy (bibby):

the actual algo is as basic as ``` quicksort(A, i, k): if i < k: p := partition(A, i, k) quicksort(A, i, p - 1) quicksort(A, p + 1, k) ``` use: quicksort(A, 1, length(A))

OpenStudy (bibby):

which part of the code do you want me to explain

OpenStudy (anonymous):

partition and quicksort function

OpenStudy (anonymous):

i got the logic but can't do the dry run...........

OpenStudy (bibby):

Let's just step through your code using a sample array 0 5 2 3 4 9 8

OpenStudy (anonymous):

ok.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!