I'm not really sure what you are doing here...
You are overloading the use of variables (using them for more than one meaning) -- and they have useless names.
The concept of a
pivot is the most difficult to understand in a quicksort -- I think that is part of your problem.
You are using an auxiliary array for something -- you don't need it.
You are implementing "passes" over the array. Quicksort doesn't work that way.
I hate to say it, but you need a fresh start. Throw that code away and try again. This FAQ should help you a lot.
http://www.cplusplus.com/faq/sequences/sequencing/sort-algorithms/quicksort/
Don't worry about any fancy pivot choice stuff -- just pick the first element. The pivot
value is the value of the indexed element. For example, given
4 6 2 1 5 7 3
choose the first element as pivot. It's value is '4'. Now shuffle all the stuff in the rest of the array so that elements less than four come before elements greater than four.
4 3 2 1 5 7 6
less ^ greater (the caret is at the last of the
less, where the indices crossed)
Swap the 4 into the correct spot. (It is now properly sorted.)
1 3 2 4 5 7 6
less ^ greater (now the caret is indexing the sorted piece of the array)
Now you need to call the function again (yes, this should all be in a function), but this time only sorting the
less and
greater parts.
1 3 2 4 5 7 6
Recurse once for each side.
1 2 3 4 5 7 6 (
less side sorted)
1 2 3 4 5 6 7 (
greater side sorted)
Since that is a simplistic example, here is one more for you to consider:
6 2 1 4 5 7 3
6 2 1 4 5 3 7 select pivot and partition, finding last of "less thans"
^
3 2 1 4 5 6 7 6 is now in the sorted spot. We will not try sorting it again.
3 2 1 4 5 6 7 We'll sort the less than 6s
3 2 1 4 5 Select pivot and partition, finding last of "less thans"
^
1 2 3 4 5 6 7 3 is now in its final, sorted spot
1 2 3 4 5 6 7 We'll sort the less than 3s
(done)
1 2 3 4 5 6 7 We'll sort the greater than 3s
(done)
1 2 3 4 5 6 7 Now that the less than 6s are done, we'll sort the greater than 6s
(there's only one element, so it is already sorted)
1 2 3 4 5 6 7 Done.
Hope this helps.