KotlinCS 124 LogoJava
PrevIndexNext
Kotlin
Java
  • Partitioning and Quicksort : 11/16/2026

  • Merge Sort : 11/13/2026

  • Sorting Algorithms : 11/12/2026

  • Working With Agents: Ask What Would Change : 11/11/2026

  • Binary Search Trees : 11/10/2026

  • Practice with Tree Recursion : 11/09/2026

  • Trees and Recursion : 11/06/2026

  • Trees : 11/05/2026

  • Recursion : 11/04/2026

  • Working With Agents: Say What You Saw : 11/03/2026

  • Working With Agents: Validate What It Built : 11/02/2026

  • Lists Review and Performance : 10/30/2026

  • Linked Lists : 10/29/2026

  • Algorithms and Lists : 10/28/2026

  • Working With Agents: Correct It, Out Loud, Early : 10/27/2026

  • Working With Agents: Discuss Ideas, Not Implementations : 10/26/2026

  • Lambda Expressions : 10/23/2026

  • Anonymous Classes : 10/22/2026

  • Practice with Interfaces : 10/21/2026

  • Implementing Interfaces : 10/20/2026

  • Using Interfaces : 10/19/2026

  • Working with Exceptions : 10/16/2026

  • Throwing Exceptions : 10/15/2026

  • Catching Exceptions : 10/14/2026

  • References and Polymorphism : 10/13/2026

  • References : 10/12/2026

  • Data Modeling 2 : 10/09/2026

  • Equality and Object Copying : 10/08/2026

  • Polymorphism : 10/07/2026

  • Inheritance : 10/06/2026

  • Data Modeling 1 : 10/05/2026

  • Static : 10/02/2026

  • Encapsulation : 10/01/2026

  • Constructors : 09/30/2026

  • Objects, Continued : 09/29/2026

  • Introduction to Objects : 09/28/2026

  • Testing and Compilation : 09/25/2026

  • Practice with Collections : 09/24/2026

  • Maps and Sets : 09/23/2026

  • Lists and Type Parameters : 09/22/2026

  • Imports and Libraries : 09/21/2026

  • Multidimensional Arrays : 09/18/2026

  • Practice with Strings : 09/17/2026

  • null : 09/16/2026

  • Algorithms and Strings : 09/15/2026

  • Strings : 09/14/2026

  • Functions and Algorithms : 09/11/2026

  • Practice with Functions : 09/10/2026

  • More About Functions : 09/09/2026

  • Errors and Debugging : 09/08/2026

  • Functions : 09/04/2026

  • Practice with Loops and Algorithms : 09/03/2026

  • Algorithms : 09/02/2026

  • Loops : 09/01/2026

  • Arrays : 08/31/2026

  • Compound Conditionals : 08/28/2026

  • Conditional Expressions and Statements : 08/27/2026

  • Operations on Variables : 08/26/2026

  • Variables and Types : 08/25/2026

  • Welcome to CS 124 : 08/24/2026

Partitioning and Quicksort

import java.util.Arrays;
import cs125.sorting.quicksort.Partitioner;
String[] array = new String[] {"you", "are", "not", "alone"};
Partitioner.partition(array);
System.out.println(Arrays.toString(array));

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!

Partitioning
Partitioning

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.

Like Mergesort, Quicksort is also based on another building block: partitioning. Let’s see how that works:

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:

// Experiment with partition

Quicksort
Quicksort

Now let’s build a recursive sorting algorithm based on Partitioner.partition! Once we have a partition method, completing the implementation is quite straightforward!

// Quicksort

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:

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.

More Practice

Need more practice? Head over to the practice page.