KotlinCS 124 LogoJava
PrevIndexNext
Kotlin
Java
  • 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

Sorting Algorithms

boolean isSorted(int[] array) {
return false;
}
assert isSorted(new int[] {1, 2, 5});

In this lesson we introduce a new topic: sorting. Sorting algorithms are both fun to implement, and fun to analyze. We’ll get to practice with iterative and recursive algorithms, and with algorithm analysis. Let’s get started!

Sorting
Sorting

Sorting is the process of putting things in order. While it may not feel at first like the most interesting problem to solve, sorting is a core building block for many other algorithms. Competitions continue to be held for systems that can sort large numbers of items. And, despite being a very old problem, new sorting algorithms continue to be released with new performance characteristics. (The sorting algorithm used by default by Java, Python, and other languages dates only to 2002.)

Sorting Basics
Sorting Basics

For the sake of simplicity most of our examples will follow a few conventions:

As we examine the performance of our sorting algorithms we’ll look at a few things:

And, while there are many sorting algorithms, we’ll focus on just a few due to their interesting performance characteristics or real world use cases. Specifically, we’ll implement or analyze:

And maybe a few more along the way!

isSorted
isSorted

As a warm up, let’s implement and analyze a simple method to determine whether an integer array is already sorted. We can use this as needed to check our work as we develop additional sorting algorithms!

// isSorted

Bubble Sort
Bubble Sort

Now let’s actually implement a sorting algorithm. Our first algorithm, Bubble Sort, follows naturally from the isSorted method that we implement above. But, rather than just returning when we find out-of-order elements, we’ll fix them! If we do this enough times, the array should end up sorted.

// Bubble Sort

Next, let’s analyze the performance of our bubble sort implementation. We’ll look at both best and worst case inputs, and try to say something general about the performance of the algorithm.

Insertion Sort
Insertion Sort

Next, let’s look at a different approach called insertion sort. You’ll implement this on the next homework problem. But let’s walk through the approach visually to get you off to a good start!

Homework: Solve Insertion Sort

Created By: Geoffrey Challen
/ Version: 2020.11.0

Create a public class named InsertionSorter. It should provide one class method sort. sort accepts an array of Comparables and sorts them in ascending order. You should sort the array in place, meaning that you modify the original array, and return the number of swaps required to sort the array as an int. That's how we'll know that you've correctly implemented insertion sort. If the array is null you should throw an IllegalArgumentException. You can assume that the array does not contain any null values.

To receive credit implement insertion sort as follows. Have the sorted part start at the left and grow to the right. Each step takes the left-most value from the unsorted part of the array and move it leftward, swapping elements until it is in the correct place. Do not swap equal values. This will make your sort unstable and cause you to fail the test suites.

CS People: Rediet Abebe
CS People: Rediet Abebe

Professor Rediet Abebe is a computer scientist and assistant professor at UC Berkeley who uses algorithms to tackle inequality and promote social justice. Born and raised in Addis Ababa, Ethiopia, she became the first Black woman to earn a Ph.D. in Computer Science from Cornell University.

Her research develops computational frameworks to address issues like poverty, educational inequality, and access to opportunity. She co-founded Black in AI, a community of over 1,500 AI researchers working to increase Black representation in the field, and Mechanism Design for Social Good (MD4SG), which uses algorithms to tackle inequality.

When she arrived in the United States to study, she was struck by educational inequalities affecting Black and low-income students even in the shadows of prestigious universities—disparities that inspired her to apply her technical skills to social problems.

More Practice

Need more practice? Head over to the practice page.