Merge Sort
> Click or hit Control-Enter to run the code above
You’re Doing Great
Keep up the good work!
There Are Many Sorting Algorithms
And we won’t discuss them all…
-
Insertion sort (today)
-
Selection sort (lab)
-
Merge sort (today)
-
Quicksort (Monday)
-
Bubble sort (lab)
-
And even new ones, like Timsort (circa 2002)
Insertion Sort: Overview
8 |
5 |
7 |
3 |
4 |
11 |
6 |
-1 |
-
Insertion sort divides the array into two parts: a sorted part and an unsorted part
-
The sorted part starts at the beginning of the array and grows during each step
Insertion Sort: Overview
8 |
5 |
7 |
3 |
4 |
11 |
6 |
-1 |
8 |
5 |
7 |
3 |
4 |
11 |
6 |
-1 |
5 |
8 |
7 |
3 |
4 |
11 |
6 |
-1 |
5 |
7 |
8 |
3 |
4 |
11 |
6 |
-1 |
3 |
5 |
7 |
8 |
4 |
11 |
6 |
-1 |
3 |
4 |
5 |
7 |
8 |
11 |
6 |
-1 |
3 |
4 |
5 |
7 |
8 |
11 |
6 |
-1 |
3 |
4 |
5 |
6 |
7 |
8 |
11 |
-1 |
-1 |
3 |
4 |
5 |
6 |
7 |
8 |
11 |
-
Insertion sort divides the array into two parts: a sorted part and an unsorted part
-
The sorted part starts at the beginning of the array and grows during each step
Insertion Sort: Insertion
8 |
5 |
7 |
3 |
4 |
11 |
6 |
-1 |
8 |
5 |
7 |
3 |
4 |
11 |
6 |
-1 |
5 |
8 |
7 |
3 |
4 |
11 |
6 |
-1 |
5 |
7 |
8 |
3 |
4 |
11 |
6 |
-1 |
3 |
5 |
7 |
8 |
4 |
11 |
6 |
-1 |
3 |
4 |
5 |
7 |
8 |
11 |
6 |
-1 |
3 |
4 |
5 |
7 |
8 |
11 |
6 |
-1 |
3 |
4 |
5 |
6 |
7 |
8 |
11 |
-1 |
-1 |
3 |
4 |
5 |
6 |
7 |
8 |
11 |
-
In each step we take the first item from the unsorted region and insert it in the right place in the sorted region
Insertion Sort: A Single Step
3 |
4 |
5 |
7 |
8 |
11 |
6 |
-1 |
-
Let’s look at one step in more detail
Insertion Sort: A Single Step
|
|
|
|
|
|
6 |
|
3 |
4 |
5 |
7 |
8 |
11 |
|
-1 |
-
Let’s look at one step in more detail
Insertion Sort: A Single Step
|
|
|
|
|
6 |
|
|
3 |
4 |
5 |
7 |
8 |
11 |
|
-1 |
-
Let’s look at one step in more detail
Insertion Sort: A Single Step
|
|
|
|
|
6 |
|
|
3 |
4 |
5 |
7 |
8 |
|
11 |
-1 |
-
Let’s look at one step in more detail
Insertion Sort: A Single Step
|
|
|
|
|
6 |
|
|
3 |
4 |
5 |
7 |
|
8 |
11 |
-1 |
-
Let’s look at one step in more detail
Insertion Sort: A Single Step
|
|
|
|
6 |
|
|
|
3 |
4 |
5 |
7 |
|
8 |
11 |
-1 |
-
Let’s look at one step in more detail
Insertion Sort: A Single Step
|
|
|
|
6 |
|
|
|
3 |
4 |
5 |
|
7 |
8 |
11 |
-1 |
-
Let’s look at one step in more detail
Insertion Sort: A Single Step
|
|
|
6 |
|
|
|
|
3 |
4 |
5 |
|
7 |
8 |
11 |
-1 |
-
Let’s look at one step in more detail
Insertion Sort: A Single Step
3 |
4 |
5 |
6 |
7 |
8 |
11 |
-1 |
-
Let’s look at one step in more detail
> Click or hit Control-Enter to run the code above
Insertion Sort Runtime
Time complexity:
-
Worst case: O(n^2) if the array is sorted in descending order (for this implementation)
-
Best case: O(n) if the array is already sorted (for this implementation)
-
Average case: O(n^2)
Space complexity: can be done in place with one temporary variable, so O(1)
Insertion Sort Runtime
Measure | Best Case | Worst Case | Average Case |
---|---|---|---|
Time |
O(n) |
O(n^2) |
O(n^2) |
Space |
O(1) |
O(1) |
O(1) |
We Can Do Better
Optimal sorting algorithms should be O(n log n) in the worst case and close to O(n) in the best case.
Java Generics (Brief Digression)
Lists are one of the two data structures you meet in heaven.
We’ve studied them in class together. But you’ll usually use Java`s built-in implementations.
import java.util.List;
import java.util.ArrayList;
import java.util.LinkedList;
List list = new ArrayList();
List anotherList = new LinkedList();
> Click or hit Control-Enter to run Example.main above
Compiler Errors v. Runtime Errors
Java and many languages that followed it have tried to transform runtime errors into compiler errors. Why?
-
You compile your code before it runs: and so before you have to demo it to a client, or before you deploy it to hundreds of users.
-
Catching errors at this stage is critical.
Generics
Java generics allow us to create reusable classes while allowing the compiler to check our code for correctness.
import java.util.List;
import java.util.ArrayList;
List<Integer> integerlist = new ArrayList<Integer>(); // This is list of Integers
List<String> stringList = new ArrayList<String>(); // This is a list of Strings
> Click or hit Control-Enter to run Example.main above
We’ll Return to Generics
And talk about how to use them in your own classes. But that’s all for today.
Merge Sort: Overview
|
|
|
|
1 |
8 |
9 |
12 |
|
|
|
|
2 |
5 |
7 |
10 |
|
|
|
|
|
|
|
|
-
Merge sort harnesses the fact that it is easy to merge two already-sorted arrays
Merge Sort: Overview
|
|
|
|
1 |
8 |
9 |
12 |
|
|
|
|
2 |
5 |
7 |
10 |
|
|
|
|
|
|
|
|
-
Merge sort harnesses the fact that it is easy to merge two already-sorted arrays
Merge Sort: Overview
|
|
|
|
8 |
9 |
12 |
|
|
|
|
|
2 |
5 |
7 |
10 |
1 |
|
|
|
|
|
|
|
-
Merge sort harnesses the fact that it is easy to merge two already-sorted arrays
Merge Sort: Overview
|
|
|
|
8 |
9 |
12 |
|
|
|
|
|
2 |
5 |
7 |
10 |
1 |
|
|
|
|
|
|
|
-
Merge sort harnesses the fact that it is easy to merge two already-sorted arrays
Merge Sort: Overview
|
|
|
|
8 |
9 |
12 |
|
|
|
|
|
5 |
7 |
10 |
|
1 |
2 |
|
|
|
|
|
|
-
Merge sort harnesses the fact that it is easy to merge two already-sorted arrays
Merge Sort: Overview
|
|
|
|
8 |
9 |
12 |
|
|
|
|
|
5 |
7 |
10 |
|
1 |
2 |
|
|
|
|
|
|
-
Merge sort harnesses the fact that it is easy to merge two already-sorted arrays
Merge Sort: Overview
|
|
|
|
8 |
9 |
12 |
|
|
|
|
|
7 |
10 |
|
|
1 |
2 |
5 |
|
|
|
|
|
-
Merge sort harnesses the fact that it is easy to merge two already-sorted arrays
Merge Sort: Overview
|
|
|
|
8 |
9 |
12 |
|
|
|
|
|
7 |
10 |
|
|
1 |
2 |
5 |
|
|
|
|
|
-
Merge sort harnesses the fact that it is easy to merge two already-sorted arrays
Merge Sort: Overview
|
|
|
|
8 |
9 |
12 |
|
|
|
|
|
10 |
|
|
|
1 |
2 |
5 |
7 |
|
|
|
|
-
Merge sort harnesses the fact that it is easy to merge two already-sorted arrays
Merge Sort: Overview
|
|
|
|
8 |
9 |
12 |
|
|
|
|
|
10 |
|
|
|
1 |
2 |
5 |
7 |
|
|
|
|
-
Merge sort harnesses the fact that it is easy to merge two already-sorted arrays
Merge Sort: Overview
|
|
|
|
9 |
12 |
|
|
|
|
|
|
10 |
|
|
|
1 |
2 |
5 |
7 |
8 |
|
|
|
-
Merge sort harnesses the fact that it is easy to merge two already-sorted arrays
Merge Sort: Overview
|
|
|
|
9 |
12 |
|
|
|
|
|
|
10 |
|
|
|
1 |
2 |
5 |
7 |
8 |
|
|
|
-
Merge sort harnesses the fact that it is easy to merge two already-sorted arrays
Merge Sort: Overview
|
|
|
|
12 |
|
|
|
|
|
|
|
10 |
|
|
|
1 |
2 |
5 |
7 |
8 |
9 |
|
|
-
Merge sort harnesses the fact that it is easy to merge two already-sorted arrays
Merge Sort: Overview
|
|
|
|
12 |
|
|
|
|
|
|
|
10 |
|
|
|
1 |
2 |
5 |
7 |
8 |
9 |
|
|
-
Merge sort harnesses the fact that it is easy to merge two already-sorted arrays
Merge Sort: Overview
|
|
|
|
12 |
|
|
|
|
|
|
|
|
|
|
|
1 |
2 |
5 |
7 |
8 |
9 |
10 |
|
-
Merge sort harnesses the fact that it is easy to merge two already-sorted arrays
Merge Sort: Overview
|
|
|
|
12 |
|
|
|
|
|
|
|
|
|
|
|
1 |
2 |
5 |
7 |
8 |
9 |
10 |
|
-
Merge sort harnesses the fact that it is easy to merge two already-sorted arrays
Merge Sort: Overview
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
2 |
5 |
7 |
8 |
9 |
10 |
12 |
-
Merge sort harnesses the fact that it is easy to merge two already-sorted arrays
> Click or hit Control-Enter to run the code above
Merge Runtime
Time complexity:
-
Worst case: O(n)
-
Best case: O(n)
-
Average case: O(n)
But What About Mergesort?
Array Recursion
1 |
10 |
5 |
6 |
4 |
11 |
7 |
-1 |
Each contiguous subarray of an array is, itself, an array.
Array Recursion
1 |
10 |
5 |
6 |
4 |
11 |
7 |
-1 |
Each contiguous subarray of an array is, itself, an array.
Array Recursion
1 |
10 |
5 |
6 |
4 |
11 |
7 |
-1 |
Each contiguous subarray of an array is, itself, an array.
Array Recursion
1 |
10 |
5 |
6 |
4 |
11 |
7 |
-1 |
Each contiguous subarray of an array is, itself, an array.
Array Recursion
1 |
10 |
5 |
6 |
4 |
11 |
7 |
-1 |
Each contiguous subarray of an array is, itself, an array.
Array Recursion
1 |
10 |
5 |
6 |
4 |
11 |
7 |
-1 |
Each contiguous subarray of an array is, itself, an array.
Array Recursion
1 |
10 |
5 |
6 |
4 |
11 |
7 |
-1 |
Each contiguous subarray of an array is, itself, an array.
Array Recursion
1 |
10 |
5 |
6 |
4 |
11 |
7 |
-1 |
Each contiguous subarray of an array is, itself, an array.
Array Recursion
1 |
10 |
5 |
6 |
4 |
11 |
7 |
-1 |
Each contiguous subarray of an array is, itself, an array.
Array Recursion
1 |
10 |
5 |
6 |
4 |
11 |
7 |
-1 |
Each contiguous subarray of an array is, itself, an array.
Array Recursion
1 |
10 |
5 |
6 |
4 |
11 |
7 |
-1 |
Each contiguous subarray of an array is, itself, an array.
Array Recursion
1 |
10 |
5 |
6 |
4 |
11 |
7 |
-1 |
Each contiguous subarray of an array is, itself, an array.
Array Recursion
1 |
10 |
5 |
6 |
4 |
11 |
7 |
-1 |
Each contiguous subarray of an array is, itself, an array.
Array Recursion
1 |
10 |
5 |
6 |
4 |
11 |
7 |
-1 |
Each contiguous subarray of an array is, itself, an array.
Array Recursion
Just like with trees and lists, we need a way to both make the problem smaller and identify the smallest subproblem.
-
How do we make the problem smaller? Break the list into two smaller subarrays.
-
What’s the smallest subproblem? An array with a single item.
Announcements
-
MP4 is out. Please get started!
-
I have office hours today from 1–3PM in Siebel 2227. Please come by and say hi!