### why doesnt this work

 1234567891011121314151617181920212223242526272829303132333435363738394041 #include ; using namespace std; void Quicksort(int info[],int left,int right){ int pivot = left + (right - left)/2;//it is the middle (will change sometimes but will end up in the middle int temp; while(left<=right){ while(info[left] < pivot){ left++; } while(info[right] > pivot){ right--; } //swap feature if(left<=right){ temp = info[left]; info[left]=info[right]; info[right] = temp; left ++; right --; } } } int main(){ int Test[20]={1,2,5,6,7,8,9,21,42,3,57,22,36,4,9,10,11,12,20,35}; Quicksort(Test,0,19); for(int i=0;i<20;i++){ cout << Test[i]<< "\n"; } system("PAUSE"); return 0; }

can someone explain why this isn't sorting
Quicksort is a recursive function. You are not calling it again after you split the array.
 12345678910111213141516171819202122232425262728293031323334353637 void Quicksort(int info[],int left,int right){ int pivot = left + (right - left)/2;//it is the middle (will change sometimes but will end up in the middle int temp; int pivotvalue = info[pivot]; temp = info[pivotvalue]; info[pivotvalue]=info[right]; info[right] = temp; while(left<=right){ while(info[left] < info[pivot]){ left++; } while(info[right] > info[pivot]){ right--; } //swap feature if(left<=right){ temp = info[left]; info[left]=info[right]; info[right] = temp; left ++; right --; } } if (left < right){ Quicksort(info,left,pivot-1); Quicksort(info,pivot+1,right); } }

I tried recursion but its still not working
Last edited on
 12345678910111213141516171819202122232425262728293031323334353637 void Quicksort(int info[],int left,int right){ int pivot = left + (right - left)/2;//it is the middle (will change sometimes but will end up in the middle int temp; int pivotvalue = info[pivot]; temp = pivotvalue; pivotvalue=info[right]; info[right] = temp; while(left<=right){ while(info[left] < info[pivot]){ left++; } while(info[right] > info[pivot]){ right--; } //swap feature if(left<=right){ temp = info[left]; info[left]=info[right]; info[right] = temp; left ++; right --; } } if (left < right){ Quicksort(info,left,pivot-1); Quicksort(info,pivot+1,right); } }

it works for left side of the pivot now but not the right?
ideas?
I would suggest referring to whatever algorithmic source you're using, because you have the algorithm wrong.
Topic archived. No new replies allowed.