- “Dummy” nodes in linked lists
- Doubly linked lists
Sorts
- Think about how one would develop a sorting algorithm from scratch:
- Find the smallest item.
- Put it at the head of the list.
- Find the next smallest item.
- …
- Notice this has a recursive feel to it:
- Find the smallest element
- Sort a shorter list
- Show Selection Sort for int array.
- Show Selection Sort for objects.
- What is the worst-case running time?
- What is the best-case running time?
- Show insertion sort
- What is the worst-case running time?
- What is the best-case running time?
- (Notice that big-O is the same)
- Show bubble sort
- What is the worst-case running time?
- What is the best-case running time?
- How can we improve the best-case?
Sorting better than n^2
- Suppose you have two sorted arrays.
- How would you combine the two arrays into a single array?
- What would the running time of this algorithm be?
- Now, suppose we did the following:
- Cut an array in half
- Sorted each half (using selection sort)
- Put the two halves together into a single sorted array.
- How fast would this be compared to the original sort?
- What if we cut into fourths? Eights?
- So, if we keep cutting it, we can sort for free right?
- Seriously, when does it stop?
- What is the worst-case running time of merge sort?
- Is there any part of merge sort that is (relatively) inefficient?
- How can we take a similar “divide and conquer” approach but, with avoiding the array copy?
- Idea: Divide first, then conquer.
- Put all the small numbers on the left and all the big numbers on the right, then recurse.
- Once each half is sorted, then the entire array will automatically be sorted.
- This approach is called quick sort. (The name should be a hint :)
- Show quicksort algorithm
- What is the worst-case running time of quick sort?
- What causes the worst-case running time?
- How can we reduce the chances of suffering the worst-case?
- examine Java’s implementation:
java/util/Arrays.java
(look for sort1
)
- Simple insertion sort if length < 7
- Median-of-three for arrays of length 7, 40
- Pseudo-median of nine for arrays of length > 40
- Example of “startup costs” of an algorithm.
- Comparator: Pass the sort algorithm as a parameter
- Sometimes objects don’t implement
Comparable
- Sometimes you want a different sort definition
Theoretical lower bound.
- Mergesort and quicksort are
O(n log n)
. Can we do better?
Radix sort
- Bucket sort
- Generalize to Radix sort
- Why doesn’t this contradict the result above?