We've been exposed to four sorting algorithms. Soon that number will be five with insertion sort on Monday, but these are the four algorithms.
Bubble Sort: Think about values bubbling up to the top.
Insertion Sort: Think about having an already sorted list and inserting new values into it.
Merge Sort: Recursive algorithm (technically all could be recursive, but I think this one is well suit), takes advantage of the fact that merging two already sorted list is easy.
Quicksort: Uses pivots and partitions to split the array apart into smaller pieces.
"Any initial questions about these algorithms?"
Again, here they are: bubble sort, insertion sort, merge sort, and quicksort.
Some components (beyond the implementation details) that you should keep in mind for the quiz:
Runtime, i.e. how fast does it finish?
Space/Memory Required, i.e. how much space does this algorithm need to run?
Best/Worse Case, i.e. to make X algorithm fast/slow, what type of array should I give it?
To practice these, we'll do some questions before coding to warm us up.
Runtime Analysis and Why (10 minutes)
For each of the algorithms, give me the runtime (worse case):
Question: What's the runtime for X and why? Talk about the component/subroutine that contributes the most to its runtime.
Your job is to construct arrays (arbitrary, but say with 10 elements) for bubble sort, insertion sort, merge sort, and quicksort that will make it very slow. With quicksort, the pivot value will be the first element in the array/subarray.
Here's a place to get started. Given a sorted array, which of these algorithms will perform poorly?
Say you're a car company and need line up cars on your lot. A good way to do that would be sorting by brand and within those brands, sort by color so they look nice. We can do that alphabetically.
Instead of writing our own sorting algorithm, we'll use Java's built-in one and implement a Comparable interface for our cars.
Remember that String has a .compareTo() method that takes another string.
System.out.println("Jackie".compareTo("Akhila")); // prints a positive number, >
System.out.println("apple".compareTo("banana")); // prints a negative number, <
System.out.println("cats".compareTo("cats")); // prints zero, ==
Sort By Multiple Attributes Starter Code
import java.util.Arrays;
publicclassCarimplementsComparable<Car> {
public String brand;
public String color;
publicCar(String setBrand, String setColor){
this.brand = setBrand;
this.color = setColor;
}
@Overridepublic String toString(){
return"(" + this.brand + ", " + this.color + ")";
}
@OverridepublicintcompareTo(Car c){
// Your code here.
}
}
Car[] carLot = new Car[]{
new Car("Toyota", "Silver"),
new Car("Suburu", "Red"),
new Car("Ford", "Red"),
new Car("Nissan", "White"),
new Car("Nissan", "Blue"),
new Car("Ford", "Black")
};
Arrays.sort(carLot);
System.out.println(Arrays.toString(carLot));
For Fun, My Favorite Sorting Algorithm (10 minutes)
BogoSort! Here's the pseudocode for it.
List<Integer> bogoSort(List<Integer> ls){
// Is it sorted?// If no, shuffle and repeat.// If yes, return array.
}
I'll give you some code that does some of this, your job is to implement it and tell me how many elements it can sort before it times out!
BogoSort Starter Code
import java.util.List;
import java.util.Arrays;
import java.util.Collections;
List<Integer> bogoSort(List<Integer> ls){
// Your code here.// You probably want to take a look at: Collections.shuffle() documentation.
}
booleanisSorted(List<Integer> ls){
// Your code here.
}
List<Integer> ls = Arrays.asList(new Integer[]{3, 2, 1});
System.out.println(ls);
System.out.println(bogoSort(ls));
That's all folks!
Another week finished, another week closer to summer break! As always hang in there (more saying that for myself, but you too)!
Now that you know some sorting algorithms, you can sort yourself out before finals! I surely need to do that.
Solution Section
Worse Case Runtimes and Justifications Solutions
Bubble sort has O(n2) runtime because each of the n elements could bubble up the array at least n−1 times which is n(n−1)=O(n2). The bubbling up/swapping processing for each element is causing the runtime to be this way.
Insertion sort has O(n2) runtime because for an array with n elemetns and at any particular index i, the number of swaps ni needs to make is i−1. The range of i depends on n so worse case scenario, the run time is O(n2) because n elements need to be swapped n times at most (rounding up i swaps).
Worse Case Runtimes and Justifications Solutions Cont.
Merge sort has to split the array log(n) times and the number of operations it performs can just be thought of as n because it depends on n. Therefore, the runtime for merge sort is O(nlog(n)).
If you pick a particularly bad pivot with quicksort, then the list will not be split. It will have to pick n pivots and do n operations and have a total runtime of O(n2).
Adversarial Examples Solutions
For bubble sort, the adversarial example would be passing in a reversed sorted list because it'll take more time for each of those elements to bubble to their correct position.
For insertion sort, exactly the same as bubble sort.
For merge sort, it doesn't matter because merge sort doesn't care.
For quicksort, depends on the pivot selection, but if the pivot is always the first element, then an already sorted list will cause it to hit the worse case runtime.