diff options
| author | mo khan <mo.khan@gmail.com> | 2020-08-27 12:05:33 -0600 |
|---|---|---|
| committer | mo khan <mo.khan@gmail.com> | 2020-08-27 12:05:33 -0600 |
| commit | 67a2c78001222fa71f391bdf48b6a95b92f11461 (patch) | |
| tree | 7f23f908e9b84f32cbedb44aad794d7a2045f4fb | |
| parent | b8f3a0b76f8add36b7f4c67025b2e4bd48eda0cc (diff) | |
Add notes
| -rw-r--r-- | doc/unit/09/README.md | 1 | ||||
| -rw-r--r-- | doc/unit/10/README.md | 31 | ||||
| -rw-r--r-- | doc/unit/11/README.md | 66 |
3 files changed, 97 insertions, 1 deletions
diff --git a/doc/unit/09/README.md b/doc/unit/09/README.md index 127665f..b6758da 100644 --- a/doc/unit/09/README.md +++ b/doc/unit/09/README.md @@ -126,4 +126,3 @@ The following properties are satisfied before and after any operation: * no-red-edge: No two red nodes are adjacent. (except the root, `u.colour + u.parent.colour >= 1`) The root is black. - diff --git a/doc/unit/10/README.md b/doc/unit/10/README.md index e69de29..1c6b645 100644 --- a/doc/unit/10/README.md +++ b/doc/unit/10/README.md @@ -0,0 +1,31 @@ +# Heaps + +Eytzinger's method can represent a complete binary tree as an array. + +`2i + 1` and the right child of the node at index `i` is at +index `right(i) = 2i +2`. The parent of the node at index `i` is at index +`parent(i) = (i-1)/2`. + +```java +class BinaryHeap { + T[] a; + int n; + + int left(int i) { + return 2*i + 1; + } + int right(int i) { + return 2*i + 2; + } + int parent(int i) { + return (i-1)/2; + } +} +``` + +A `BinaryHeap` uses this technique to implicitly represent a complete +binary tree in which the elements are `heap-ordered.` + +heap-ordered: The value stored at any index `i` is not smaller than the value stored at index `parent(i)`, with the exception of the root value, `i = 0`. +The smallest value in the priority Queue is at position 0. + diff --git a/doc/unit/11/README.md b/doc/unit/11/README.md index e69de29..7e0a37e 100644 --- a/doc/unit/11/README.md +++ b/doc/unit/11/README.md @@ -0,0 +1,66 @@ +# Comparison-Based Sorting + +* merge sort O(nlogn) +* quick sort O(nlogn) +* heap sort O(nlogn) + +`compare(a,b)` + +* -1: a < b +* 0: a == b +* +1: a > b + +## Merge-Sort + +is a classic recursive divide and conquer. + +* if `a` is at most 1 then `a` is sorted. +* else we split `a` into two halves + * `a0 = a[0...(n/2)-1]` + * `a1 = a[(n/2)...n-1]` + * merge a0, a1 + +```java +void mergeSort(T[] a, Comparator<T> c) { + if (a.length <= 1) return; + + T[] a0 = Arrays.copyOfRange(a, 0, a.length/2); + T[] a1 = Arrays.copyOfRange(a, a.length/2, a.length); + mergeSort(a0, c); + mergeSort(a1, c); + merge(a0, a1, a, c); +} +``` + +## Quicksort + +Unlike mergesort which does merging after solving the two subproblems, +quicksort does all of its work upfront. + +1. pick a random `pivot` element `x` from `a` +2. partition `a` into the set of elements less than `x`, the set of elements equal to `x` and the set of elements greater than `x`. +3. recursively sort the first and third sets in this partition. + +```java +void quickSort(T[] a, Comparator<T> c) { + quickSort(a, 0, a.length, c); +} +void quickSort(T[] a, int i, int n, Comparator<T> c) { + if (n <= 1) return; + T x = a[i + rand.nextInt(n)]; + int p = i - 1, j = i, q = i+n; + + while (j < q) { + int comp = compare(a[j], x); + if (comp < 0) { + swap(a, j++, ++p); + } else if (comp > 0) { + swap(a, j, --q); + } else { + j++; + } + } + quickSort(a, i, p-i+1, c); + quickSort(a, q, n-(q-i), c); +} +``` |
