We continue our discussion of sorting algorithms by introducing the wild child: Quicksort.
Quicksort can achieve best-case sorting behavior while using less space than Mergesort.
In this lesson we’ll see how it works, starting with the building block it relies on: partitioning.
Let’s get started!
Mergesort was our first recursive sorting algorithm.
It employed a bottom-up approach—first breaking the array into individual chunks, and then merging them back together.
Quicksort is another recursive approach, but it works differently.
Let’s first examine its operation at a high level, and then break it down further.
Content Restricted to Current CS 124 Students
A publicly-accessible version of this content is available at learncs.online.
Like Mergesort, Quicksort is also based on another building block: partitioning.
Let’s see how that works:
Content Restricted to Current CS 124 Students
A publicly-accessible version of this content is available at learncs.online.
You’ll get to complete partition yourself on this lesson’s homework!
But we can at least experiment with it using the method built-in to our playground:
Content Restricted to Current CS 124 Students
A publicly-accessible version of this content is available at learncs.online.
Now let’s build a recursive sorting algorithm based on Partitioner.partition!
Once we have a partition method, completing the implementation is quite straightforward!
Content Restricted to Current CS 124 Students
A publicly-accessible version of this content is available at learncs.online.
// Quicksort
Problem Restricted to Current CS 124 Students
A publicly-accessible version of this content is available at learncs.online.
Practice: Solve Is Partitioned
Created By: Geoffrey Challen
/ Version: 2026.10.0
Write a method isPartitioned that accepts an array of int values and an int index, and returns a boolean
indicating whether the array is correctly partitioned around the pivot at that index.
An array is correctly partitioned around the pivot if every value that comes before the pivot is smaller than
the pivot, and every value that comes after the pivot is larger than or equal to the pivot.
This is exactly what a partition method should leave behind, so you could use isPartitioned to test one!
The array passed to isPartitioned will not be null or empty, and the index will always be a valid index into
the array.
Note that you need to check every value in the array, not just the values right next to the pivot.
Here are some examples:
For this problem we're expecting only a method.
Don't define a class, include modifiers like static or public, add import statements, or add code outside the method declaration.
Problem Restricted to Current CS 124 Students
A publicly-accessible version of this content is available at learncs.online.
Homework: Solve Quicksort Partition (First Value)
Created By: Geoffrey Challen
/ Version: 2020.6.0
Create a public, non-final class named Partitioner. Implement a public static method
int partition(int[] values) that returns the input array partitioned using the first
array value as the pivot. All values smaller than the pivot should precede it in the array,
and all values larger than or equal to the pivot should follow it. Your method should return the
index of the pivot value. If the array is null or empty you should return -1.