3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 53, 9, and 5 are the three values selected as potential pivot candidates. 3 and 5 are the left and rightmost elements in the array and 9 is the center element:
a[center] = a[(left + right)/2] = a[(0 + 10)/2] = a[5] = 9
3 and 9 are the first two values compared because they are the left
and center values. They are in the proper order so we proceed to
the next step, where the left and right values are compared, which
are 3 and 5. Again the values are in the correct order. The final
test for determining the pivot is to compare the current center
and right values, which are 9 and 5. Since 5 is less than 9, 5 and 9
swap positions:
3, 1, 4, 1, 5, 5, 2, 6, 5, 3, 9
Next we place the pivot aside by swapping it with the next to last
element in the array, which is 3:
3, 1, 4, 1, 5, 3, 2, 6, 5, 5, 9
The leftmost and rightmost elements, 3 and 9 respectively, are now
in the correct partition and 5 has been set aside until the end. Hence
we set our left index to 1 and our right index to the first element to
the left of our pivot.
Our initial configuration is then:
| Initial configuration | 3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5 |
| Step 1: swap 1 & 3 | 1, 3, 4, 1, 5, 9, 2, 6, 5, 3, 5 |
| Step 2: 4 is in the right place | 1, 3, 4, 1, 5, 9, 2, 6, 5, 3, 5 |
| Step 3: insert 1 after the first 1 | 1, 1, 3, 4, 5, 9, 2, 6, 5, 3, 5 |
| Step 4: 5 is in the right place | 1, 1, 3, 4, 5, 9, 2, 6, 5, 3, 5 |
| Step 5: 9 is in the right place | 1, 1, 3, 4, 5, 9, 2, 6, 5, 3, 5 |
| Final configuration after 5 steps | 1, 1, 3, 4, 5, 9, 2, 6, 5, 3, 5 |