Skip to content
All apps

Quicksort: Partitions

About this app

Quicksort as on the slides: the last element becomes the pivot, every element before it goes into the first or second partition with one sentence, the pivot swaps in between and stays at its correct position. Then the sub-ranges with the same method, including trivial and empty ones. Tiles glide when swapped, below them a staircase of the calls; Python code with the running line, counters, predictions. Example 2, 7, 6, 8, 4, 1, 3, 5 with 13 comparisons; sorted and reversed input are the worst case with n(n−1)/2.

With explain mode: an AI tutor walks you through the app, points out what matters in the scene and answers your questions.

Split instead of sort

Quicksort chooses a pivot element, here, as in the lecture, the last element of a range. Every element before it is compared with it: whatever is smaller or equal goes into the first partition, whatever is larger into the second. Finally the pivot swaps with the first element of the second partition and is then in its final position. Then Quicksort sorts both parts with the same method. A range with one element is already sorted. On average this takes O(n log n) comparisons, in the worst case n(n−1)/2.

What the app shows

A row of tiles with indices, the pivot, the first and second partition and the unsorted part in their own colours, with labels above as on the slides. Each step shows the sentence of the slide, such as “4 is smaller than 5, goes into the first partition. Swap 7 and 4.” When swapping, the tiles glide past each other in an arc. Finished elements appear as a bare number. A staircase shows every recursive call as a bar, one row per depth. Alongside runs the Python code with the current line and the call stack.

Try it yourself

Step through the slide example 2, 7, 6, 8, 4, 1, 3, 5 or play it: 13 comparisons, the staircase stays four rows flat. With the sorted list, a worst case, the pivot stays on the right every time, the staircase becomes as deep as the list is long, and eight numbers take 28 comparisons. Predictions ask before every comparison which partition an element goes into. Your own numbers, many equal values and random lists of up to twelve numbers are possible.

Frequently asked questions

How does partitioning work in Quicksort?
In Lomuto's partitioning the last element of the range is the pivot. A loop goes through the elements before it; if an element is smaller than or equal to the pivot, it is swapped with the first element of the second partition (with itself while that is still empty). Finally the pivot swaps with the first element of the second partition and is in its final position.
Why is a sorted list a worst case for Quicksort?
If the last element is the pivot, on a sorted list it is the largest. All other elements end up in the first partition and the second stays empty. Each range only gets one element shorter, the recursion is n levels deep, and there are n(n−1)/2 comparisons, that is O(n²). A reversed list costs just as much.
Is Quicksort stable?
No. Swapping can change the order of equal values. With the comparison a[j] <= x, all values equal to the pivot also go into the first partition, so many equal values make the split lopsided.
What does the app Quicksort: Partitions show?
The Quicksort example from the lecture slides as a row of tiles with first and second partition, pivot and the sentence of the slide for every step, plus the Python code with the running line, a staircase of the recursive calls, counters, prediction questions and your own lists.

Subject: Computer science

More from Computer science

A VisuApp by heyprof: interactive, free in your browser, no sign-up. What is a VisuApp? · All apps