c-programming quick sort question why is the value of left and right changing ?
i made it but am stuck in the dry tun :/ help anyone ?
-run
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); } ```
sorry bout that ....... can u tell me why are the left and right variable values changing ? the dry run is not as i expected
you might want to use a helper function to partition, I'm comparing your code to some working code right now
i know aboutt the partition function but i can't get it ....... that's why i made this (without partition)
I'm too braindead to step through your code right now. Examine this http://www.comp.dit.ie/rlawlor/Alg_DS/sorting/quickSort.c
ahhh D: the problem is i can never understand other's code so i make up my own logic :/
and now i am stuck ....................
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))
which part of the code do you want me to explain
partition and quicksort function
i got the logic but can't do the dry run...........
Let's just step through your code using a sample array 0 5 2 3 4 9 8
ok.
Join our real-time social learning platform and learn together with your friends!