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

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

More Practice

Need more practice? Head over to the practice page.