I am currently studying quicksort and would like to know how it works when the first (or last) element is chosen as the pivot point.
Say for example I have the following array:
{15, 19, 34, 41, 27, 13, 9, 11, 44}
This is what I think happens:
{15, 19, 34, 41, 27, 13, 9, 11, 44}
^
pivot
{15, 19, 34, 41, 27, 13, 9, 11, 44}
^ ^
compare these two, they are good
{15, 19, 34, 41, 27, 13, 9, 11, 44}
^ ^
compare these two and swap
{11, 19, 34, 41, 27, 13, 9, 15, 44}
^ ^
compare these two and swap
{9, 19, 34, 41, 27, 13, 11, 15, 44}
^ ^
compare these two, they are good
{9, 19, 34, 41, 27, 13, 11, 15, 44}
^ ^
compare these two, they are good
{9, 19, 34, 41, 27, 13, 11, 15, 44}
^ ^
compare these two, they are good
{9, 19, 34, 41, 27, 13, 11, 15, 44}
^ ^
compare these two, they are good
{9, 19, 34, 41, 27, 13, 11, 15, 44}
^ ^
compare these two, they are good
{9, 19, 34, 41, 27, 13, 11, 15, 44}
End of first partition
Is this how it works? If so, would 19 be the new pivot point, or do you divide the array in half to find it (so that it would be 27/13), or does it depend on the implementation of the quicksort? Thanks for your time!
Pivot element can be any element from the array, it can be the first element, the last element or any random element.
A large array is partitioned into two arrays one of which holds values smaller than the specified value, say pivot, based on which the partition is made and another array holds values greater than the pivot value. Quicksort partitions an array and then calls itself recursively twice to sort the two resulting subarrays.
Quicksort is a divide-and-conquer algorithm. It works by selecting a 'pivot' element from the array and partitioning the other elements into two sub-arrays, according to whether they are less than or greater than the pivot. For this reason, it is sometimes called partition-exchange sort.
Check wikipedia, there is a little example with a bit smaller list of inplace quicksort http://en.wikipedia.org/wiki/Quicksort
With your example the idea is to partition
{15, 19, 34, 41, 27, 13, 9, 11, 44}
into
{13, 9, 11 -- 15 -- 19, 34, 41, 27, 44}
So first we move pivot to the end
Swap 44, and 15
{44, 19, 34, 41, 27, 13, 9, 11, 15}
^ ^
Than check 44, its larger than pivot, so swap with one one before last...
{11, 19, 34, 41, 27, 13, 9, 44, 15}
^ ^
than check element at some position as last one was larger than pivot.
9 < 15, so proceed to the next, 19 > 15 => swap
{11, 9, 34, 41, 27, 13, 19, 44, 15}
^ ^
swap again
{11, 9, 13, 41, 27, 34, 19, 44, 15}
^ ^
next
{11, 9, 13, 41, 27, 34, 19, 44, 15}
^ ^
and second last swap
{11, 9, 13, 27, 41, 34, 19, 44, 15}
^
Now as forward and backward indices reached each other,
we swap pivot into right position
{11, 9, 13, 15, 41, 34, 19, 44, 27}
And we got partitioned set. Items less than 15 at the beginning, than pivot = 15, and then greater elements.
EDIT: algorithm described in wikipedia article is a bit different:
Legend:
^ = storeindex
# = i
{44, 19, 34, 41, 27, 13, 9, 11, 15}
^#
{44, 19, 34, 41, 27, 13, 9, 11, 15}
^ #
... until ...
{44, 19, 34, 41, 27, 13, 9, 11, 15}
^ #
{13, 19, 34, 41, 27, 44, 9, 11, 15}
^ #
{13, 9, 34, 41, 27, 44, 19, 11, 15}
^ #
{13, 9, 11, 41, 27, 44, 19, 34, 15}
^ #
{13, 9, 11, 15, 27, 44, 19, 34, 41}
^- pivot
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With