Sort 7 3 6 4 2 1 5 with quick-sort, always taking the middle element (index = length / 2, integer division) as pivot. What does the tree look like?
Pivot 4 (index 3) splits the input into $L$ = 3 2 1 and $G$ = 7 6 5. Both have their median in the middle (2 and 6), so the next split leaves single elements: a perfectly balanced tree with two levels of partitioning.
* With 7 elements the middle index is 7 / 2 = 3, with 3 elements it is 3 / 2 = 1. *
Step by step:
- 7 3 6 4 2 1 5 has length 7, pivot index 7 / 2 = 3, so the pivot is 4. $L$ = 3 2 1 (input order kept), $E$ = 4, $G$ = 7 6 5.
- 3 2 1: index 3 / 2 = 1, pivot 2, leaving 1 and 3. Result 1 2 3.
- 7 6 5: index 1, pivot 6, leaving 5 and 7. Result 5 6 7.
- Concatenate: 1 2 3, then 4, then 5 6 7.
That costs $6 + 2 + 2 = 10$ comparisons with a pivot, the best case for seven elements. The balance is luck of this input, though: here the middle position happens to hold the median every time, and nothing guarantees that in general. For an even length the integer division picks the upper of the two middle elements: 4 / 2 = 2, the third of four.
Go deeper:
Quicksort: choice of pivot โ first, middle, random and median-of-three pivots compared.