Conditional Expressions and Statements : 08/24/2023
Operations on Variables : 08/23/2023
Variables and Types : 08/22/2023
Welcome to CS 124 : 08/21/2023
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 utilize 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.
https://www.youtube.com/watch?v=NUFxa2wCSH4
Finally, let’s analyze the performance of Mergesort.
This is an interesting case!
Let’s walk through it carefully.
https://www.youtube.com/watch?v=lcAXXvl3c3U
Homework: Mergesort
Created By: CS 124 Staff
/ Version: 2020.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.
You should use an array of size 1 or 0 as your base case.
This problem deadline has passed, but you can continue to practice. Experiment! You will not lose credit.
Homework Restricted to Current CS 124 Students
A publicly-accessible version of this content is available at learncs.online.