Quick sort
A sorting algorithm that picks a pivot, puts smaller values on one side and larger on the other, and repeats with each side. O(n log n) on average and in place.
Example
With 1 million items it takes about 20 million steps on average, with no extra array.
Test yourself: Quick sort
Quick sort
After partitioning with pivot 3, everything to its left is ___ 3.
With [3, 7, 1, 5, 2], partitioning gives [1, 2, 3, 5, 7]: 1 and 2 on the left, 5 and 7 on the right.
Quick sort
Quick sort stops partitioning a piece when it has ___ element.
A 1-element piece is already sorted. With 1,000 elements, the recursion goes down until there are 1,000 pieces of 1.
Quick sort
With pivots splitting exactly in half, quick sort with 1,024 elements has about ___ levels.
Each level splits in half: 1,024, 512, ... down to 1, in 10 levels. At each one it walks all 1,024: about 10,240 steps.
0/3
â–¶ PlayLessons that use it
Time complexity
Count steps, not seconds
Arrays
Adjacent cells
What makes a good sort
5 things you ask of it
Insertion sort
Like arranging the cards in your hand
Quick sort
A pivot and 2 sides
Merge sort
Split in half and merge in order
Heap sort
Take the maximum and leave it at the end
Divide and conquer
Split, solve and combine
