KotlinCS 124 LogoJava
PrevIndexNext
Kotlin
Java
  • Quicksort Performance and Sorting Review : 11/17/2026

  • 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

Quicksort Performance and Sorting Review

Quicksort can achieve best-case sorting behavior while using less space than Mergesort. But, Quicksort also has some pathological cases we need to understand. Once we’ve explored them, we’ll review all of the sorting algorithms that we’ve learned together, and the tradeoffs between them.

Warm Up Debugging Challenge
Warm Up Debugging Challenge

You knew it was coming…

Create a public class named BinaryTreeValuesAtLevel providing a single static method valuesAtLevel. valuesAtLevel accepts a BinaryTree<Comparable> (from cs125.trees) and a level as an int. It should return, as a List<Comparable>, all the values in the passed tree that are at the passed level, sorted in ascending order. If the passed tree is null, or the level is negative, you should throw an IllegalArgumentException.

The level corresponds to the depth of the node in the tree. So the root node is at level 0, its children are at level 1, its children's children are at level 2, etc.

To complete this question you probably need a helper method. Our suggestion is that the main valuesAtLevel create the list, begin the recursion, and sort the result. (Just use a built-in sort.) Your recursive method will need to track both the current level in the tree and the target level. Once the target level is reached, the current node's value should be added and the recursion can stop. You will also want to pass the list created by your main method.

Instructions

Quicksort Performance
Quicksort Performance

Let’s use the Quicksort implementation we completed last time to experiment with its performance. Surprises are in store!

// Quicksort Performance Analysis
import java.util.Random;
import java.util.Arrays;
Integer[] randomIntArray(int length) {
Random random = new Random();
Integer[] results = new Integer[length];
for (int i = 0; i < results.length; i++) {
results[i] = random.nextInt(128);
}
return results;
}
System.out.println(Arrays.toString(randomIntArray(8)));

What’s going on here? Let’s consider things visually.

Practice: Solve Median of Three

Created By: Geoffrey Challen
/ Version: 2026.10.0

When Quicksort always uses the first value in the array as its pivot, an array that is already sorted causes its worst-case performance. One common fix is to choose the pivot more carefully, using the median of three: look at the first value, the middle value, and the last value in the array, and use whichever one falls in between the other two.

Write a method medianOfThree that accepts an array of int values and returns an int: the median of the array's first value, its middle value, and its last value. The middle value is the value at index values.length / 2. The median is the value that would be in the middle if you put those three values in order. The three values may include duplicates, and for very short arrays some of them may come from the same position in the array. The array passed to medianOfThree will not be null or empty.

Here are some examples:

Sorting Algorithm Review
Sorting Algorithm Review

Our next quiz will focus on sorting algorithms, and particularly the ones that we’ve covered together:

Let’s review salient aspects of sorting performance. Specifically: best and worst case inputs and runtime, and memory usage.

Sorting Tradeoffs
Sorting Tradeoffs

Sorting algorithms represent a fascinating set of tradeoffs between different performance attributes. For example:

Timsort, the default sorting algorithm used by several languages including Python and Java, actually combines elements of both insertion sort and Mergesort, in addition to some other tricks.

Sorting Stability
Sorting Stability

As a final note, let’s discuss sort algorithm stability. Stability is a desirable property of sorting algorithms. It means that items with equal values will not change positions while the array is sorted.

Why is stability desirable? Because it allows us to run a sorting algorithm multiple times on complex data and produce meaningful results.

For example, imagine that you want a list of restaurants sorted first by cuisine and then by name. Assuming our sorting algorithm is stable, we can accomplish this by first sorting the list by name and then by cuisine. However, if the sorting algorithm is unstable that second sort by cuisine will destroy the results of the sort by name, and render the combination meaningless.

More Practice

Need more practice? Head over to the practice page.