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
Merge Sort
int[] merge(int[] first, int[] second) {
returnnull;
}
This lesson continues our exploration of sorting by examining a new approach.
We’ll start with a simple but powerful observation, and then examine how to build on it to create a complete sorting algorithm.
This will also represent our first sorting algorithm that achieves best-case sorting performance!
What are we waiting for?
Next let’s consider how to design a sorting algorithm that utilizes our merge method.
We’ll also use this as a chance to point out how we can apply recursive algorithms on arrays, rather than trees, which we’ve used in the past.
Content Restricted to Current CS 124 Students
A publicly-accessible version of this content is available at learncs.online.
Finally, let’s analyze the performance of Mergesort.
This is an interesting case!
Let’s walk through it carefully.
Content Restricted to Current CS 124 Students
A publicly-accessible version of this content is available at learncs.online.
Problem Restricted to Current CS 124 Students
A publicly-accessible version of this content is available at learncs.online.
Homework: Solve Mergesort
Created By: Geoffrey Challen
/ Version: 2026.11.0
Create a public class named Mergesort that provides a single instance method (this is required for testing)
named mergesort.
mergesort accepts an array of ints and returns a sorted (ascending) array.
You should not modify the passed array.
If the array that is passed is null you should throw an IllegalArgumentException.
Mergesort should extend Merge, and its parent provides several helpful methods:
int[] merge(int[] first, int[] second): this merges two sorted arrays into a second sorted array. If either
array is null it throws an IllegalArgumentException, so don't call it on null arrays.
int[] copyOfRange(int[] original, int from, int to): this acts as a wrapper on java.util.Arrays.copyOfRange,
accepting the same arguments and using them in the same way.
(You can't use java.util.Arrays in this problem for reasons that will become obvious if you inspect the rest of
the documentation...)
Note that you do need to use merge and call it the correct number of times.
This will be tested during grading.
Your solution must be recursive: mergesort should split the array in half, call itself on each half, and merge
the two sorted results.
You should use an array of size 1 or 0 as your base case.