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 S[1] (17) into its appropriate position.
Example: [17, -10, 7, 19, 21, 23, -13, 31, 59].
^# = pivot
^ pointer
My understanding:
17, -10, 7, 19, 21, 23, -13, 31, 59
^# ^
Comparison 1. No swap.
17, -10, 7, 19, 21, 23, -13, 31, 59
^# ^
Comparison 2. No swap.
17, -10, 7, 19, 21, 23, -13, 31, 59
^# ^
Comparison 3. Swap.
-13, -10, 7, 19, 21, 23, 17, 31, 59
^ ^#
Comparison 4. Swap.
-13, -10, 7, 19, 21, 17, 23, 31, 59
^ ^#
Comparison 5. Swap.
-13, -10, 7, 19, 17, 21, 23, 31, 59
^ ^#
Comparison 6. Swap.
-13, -10, 7, 17, 19, 21, 23, 31, 59
^ ^#
Comparison 7. No swap.
-13, -10, 7, 17, 19, 21, 23, 31, 59
^ ^#
Comparison 9. No swap.
-13, -10, 7, 17, 19, 21, 23, 31, 59
^ ^#
Comparison 10. No swap.
Is this how it works? Would it take 10 comparisons and 4 swaps to move pivot S[1] (17) into the correct position?