LOGBOOK

HELP

Quiz Entry - updated: 2026.12.02

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.

Quick-sort of 7 3 6 4 2 1 5 with the middle element as pivot: 4 at the root, then 2 and 6, then single elements

* With 7 elements the middle index is 7 / 2 = 3, with 3 elements it is 3 / 2 = 1. *

Step by step:

  1. 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.
  2. 3 2 1: index 3 / 2 = 1, pivot 2, leaving 1 and 3. Result 1 2 3.
  3. 7 6 5: index 1, pivot 6, leaving 5 and 7. Result 5 6 7.
  4. 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:

From Quiz: ADS / Merge-Sort and Quick-Sort | Updated: Dec 02, 2026