KotlinCS 124 LogoJava
PrevIndexNext
Kotlin
Java
  • Implementing a Map : 04/26/2024

  • Streams : 04/25/2024

  • Generics : 04/24/2024

  • Hashing : 04/23/2024

  • Binary Search : 04/22/2024

  • MP3: Course Ratings : 04/19/2024

  • Quicksort : 04/18/2024

  • Merge Sort : 04/17/2024

  • Sorting Algorithms : 04/16/2024

  • MP Debugging Part 1 : 04/15/2024

  • MP2: Course Activity : 04/12/2024

  • Practice with Recursion : 04/11/2024

  • MP Debugging Part 0 : 04/10/2024

  • MP2: API Client : 04/09/2024

  • MP2: API Server : 04/08/2024

  • Trees and Recursion : 04/05/2024

  • Trees : 04/04/2024

  • Recursion : 04/03/2024

  • MP1: Filtering and Search : 04/02/2024

  • MP1: Loading and Sorting : 04/01/2024

  • Lists Review and Performance : 03/29/2024

  • Linked Lists : 03/28/2024

  • Algorithms and Lists : 03/27/2024

  • Continuing MP0 : 03/26/2024

  • Getting Started with MP0 : 03/25/2024

  • Lambda Expressions : 03/22/2024

  • Anonymous Classes : 03/21/2024

  • Practice with Interfaces : 03/20/2024

  • Implementing Interfaces : 03/19/2024

  • Using Interfaces : 03/18/2024

  • Working with Exceptions : 03/08/2024

  • Throwing Exceptions : 03/07/2024

  • Catching Exceptions : 03/06/2024

  • References and Polymorphism : 03/05/2024

  • References : 03/04/2024

  • Data Modeling 2 : 03/01/2024

  • Equality and Object Copying : 02/29/2024

  • Polymorphism : 02/28/2024

  • Inheritance : 02/27/2024

  • Data Modeling 1 : 02/26/2024

  • Static : 02/23/2024

  • Encapsulation : 02/22/2024

  • Constructors : 02/21/2024

  • Objects, Continued : 02/20/2024

  • Introduction to Objects : 02/19/2024

  • Compilation and Type Inference : 02/16/2024

  • Practice with Collections : 02/15/2024

  • Maps and Sets : 02/14/2024

  • Lists and Type Parameters : 02/13/2024

  • Imports and Libraries : 02/12/2024

  • Multidimensional Arrays : 02/09/2024

  • Practice with Strings : 02/08/2024

  • null : 02/07/2024

  • Algorithms and Strings : 02/06/2024

  • Strings : 02/05/2024

  • Functions and Algorithms : 02/02/2024

  • Practice with Functions : 02/01/2024

  • More About Functions : 01/31/2024

  • Errors and Debugging : 01/30/2024

  • Functions : 01/29/2024

  • Practice with Loops and Algorithms : 01/26/2024

  • Algorithms : 01/25/2024

  • Loops : 01/24/2024

  • Arrays : 01/23/2024

  • Compound Conditionals : 01/22/2024

  • Conditional Expressions and Statements : 01/19/2024

  • Operations on Variables : 01/18/2024

  • Variables and Types : 01/17/2024

  • Welcome to CS 124 : 01/16/2024

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. But, Quicksort also has some pathological cases we need to understand. Let’s get started!

Warm Up Debugging Challenge
Warm Up Debugging Challenge

You knew it was coming…

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 on our next homework! But we can at least experiment with it using the method built-in to our playground:

// Experiment with partition

Quicksort
Quicksort

Next, first we’ll implement Quicksort. Then we’ll analyze its performance!

Implementation
Implementation

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

// Quicksort

Performance Analysis
Performance Analysis

Next, let’s use the Quicksort implementation we completed above 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.

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 in unstable that second sort by cuisine will destroy the results of the sort by name, and render the combination meaningless.

Homework: 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.