### QuickSort problem

hello, i am a c++ noob, here is my work on quick sort problem,
i don't know why it doesn't work, any one can point out the mistake will be very thankful.
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283`` ``````#include using namespace std; void Swap(int,int); void QuickSort (int [], int, int) int main() { int array[] = {693, 600, 766, 60, 203, 700, 567, 568, 216, 331}; cout <<"the original sequence is:" <=pivotPlace; i--) { checkPt = i; if (array[i] < array[pivotPlace]) { Swap (array[i], array[pivotPlace]); start = pivotPlace; checkPt =-1; pivotPlace = i; break; } } if (checkPt == pivotPlace) checkPt = pivotPlace; else { for(int j=start+1; j<=pivotPlace; j++) { checkPt = j; if (array[j] > array[pivotPlace]) { Swap (array[j], array[pivotPlace]); start = pivotPlace; checkPt = -1; pivotPlace = j; break; } } } } QuickSort (array, x, pivotPlace-1); QuickSort (array, pivotPlace+1, y); } ``````
Last edited on
Your Swap function should look like this:
 ``123456`` ``````void Swap(int& x,int& y) { int temp = x; x = y; y = temp; }``````

Notice the ampersand (&) signs? They tell the compiler that any changes made to x and y should be applied to the original variables that you passed to the function. This is called passing by reference.

Here an explanation in detail: http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/ and here a general explanation about references: http://www.learncpp.com/cpp-tutorial/611-references/
Last edited on
@ fransje: or just use std::swap
http://www.cplusplus.com/reference/algorithm/swap/
still doesn't work

it seems some wrong in the QuickSort block
however i cant figure it out
attach the debugger and see where you are going wrong.
swap code should be like this
 ``123456`` ``````void Swap(int* x,int* y) { int temp =* x; *x = *y; *y = temp; }``````

and call it like this

`Swap (&array[i], array[&pivotPlace]);`
thanks all

i revised the swap function and the line 43
 ``12`` `````` if (x == y) return;``````

to be like this
 ``12`` ``````if (x >= y) return;``````

than it works
Topic archived. No new replies allowed.