liveBook · Manning

Pivot

As explained in Software Design for Python Programmers

A pivot element is the selected list element whose value divides the elements being processed into lower and higher partition regions.

A pivot is the selected list element whose value divides the elements being processed into lower and higher partition regions. After partitioning, the pivot occupies its final sorted position. section 15.6 section 15.6.2

A partition uses the pivot as follows:

  • Choose an element, commonly near the middle, and move it temporarily to the right. section 15.6.2
  • Scan from both sides, exchanging values so elements less than or equal to the pivot move left and greater values move right. section 15.6.2
  • Return the pivot index, then recursively quicksort the sublists on either side. section 15.6.3

Partitioning around 52

Selecting 52 and partitioning places every value less than or equal to 52 on its left and every greater value on its right; both sublists remain to be sorted. section 15.6.2

Figure 15.12 Choosing 52 (circled) to be the pivot value and then partitioning the list into two sublists. Variables i and j are list indexes. After partitioning, all values to the left of 52 are less than or equal to it, and all values to the right of 52 are greater. The two boxed sublists on either side of the pivot are now ready to be recursively quicksorted.
A screenshot of a math test

AI-generated content may be incorrect.

Use the figure to trace pivot selection, index movement, exchanges, and the resulting recursive sublists. section 15.6.2

How Software Design for Python Programmers covers it

How deep it goes
Explains selection, parking, bidirectional scanning, swapping, and final placement, then stops short of pivot-selection alternatives.
Examples
Uses pivots 16, 52, 33, 7, 71, and 65 in list-partitioning demonstrations.
What it assumes
Assumes list indexing and recursion while establishing partition boundaries and base cases through the sorting procedure.
What you can do after
Build quicksort partitioning that returns a pivot index and recursively sorts the remaining sublists.
How it connects
Connects pivoting with quicksort, partitioning, recursion, sublists, and base cases.

Also explained in

Compare how 4 books explain pivot →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Software Design for Python Programmers. The section links point to the parts of the book it draws on.