I'm studying Quick-Sort and I am confused as to how it works when the first element is chosen as the pivot point.
I am trying to trace the first step in the Quick-Sort algorithm, to move the pivot (15) into its appropriate position.
Example: [15, -7, 22, 7, 21, 34, -13, 19]
My understanding:
15, -7, 22, 7, 34, -13, 19
# ^ ^
Move left-pointer to first element larger than the pivot. 1 comparison (move to 22). Move right-pointer to first element smaller than the pivot. 1 comparison (move to -13).
15, -7, 22, 7, 34, -13, 19
# ^ ^
Swap elements at the pointers (22, -13).
15, -7, -13, 7, 34, 22, -13, 19
# ^ ^
Move left-pointer to first element larger than the pivot. 2 comparisons (move to 34). Unable to move right-pointer to first element smaller than the pivot (cannot past the left-pointer.
15, -7, -13, 7, 34, 22, -13, 19
# ^ ^
Swap elements at the pointers (34, 22).
15, -7, -13, 7, 22, 34, -13, 19
# ^ ^
This is where I am stuck. My understanding is that the pivot should then be swapped with 22, but that doesn't seem correct?
How do I move the pivot (15) into its appropriate position?