1.02k likes | 1.22k Views
Thinking about Algorithms Abstractly. Sorting & Lower Bounds. Jeff Edmonds York University. Lecture 5. COSC 3101. Assignment 2. Now available on website. DUE: February 13th. CORRECTION:. Problem 1: Replace... while x!=0 AND y!=0 AND z!=0 do with...
E N D
Thinking about Algorithms Abstractly Sorting & Lower Bounds Jeff Edmonds York University Lecture5 COSC 3101
Assignment 2 • Now available on website. • DUE: February 13th CORRECTION: Problem 1: Replace... while x!=0 AND y!=0 AND z!=0 do with... while x!=0 OR y!=0 OR z!=0 do
Four Recursive Sorts Size of Sublists n/2,n/2 n-1,1 Minimal effort splitting Lots of effort recombining Lots of effort splittingMinimal effort recombining
Search array for minimum item Swap with first item Search remaining array for min item Swap with first item 5, 9, 14, 31, 25, 8, 18 25, 9, 14, 31, 5, 8, 18 5, 9, 14, 31, 25, 8, 18 5, 8, 14, 31, 25, 9, 18 5, 8, 9, 14, 18, 25, 31 Selection Sort … Loop …
Insert 1st item in correct position Insert 2nd item in correct position 25, 9, 14, 31, 5, 8, 18 9, 25, 14, 31, 5, 8, 18 9, 14, 25, 31, 5, 8, 18 5, 8, 9, 14, 18, 25, 31 Insertion Sort Insert 3rd item in correct position … Loop …
Selection Sort Q(n2) Average & Worst Time: Insertion Sort Q(n2) Average & Worst Time:
Divide and Conquer 52 88 14 31 98 25 30 23 62 79 Merge Sort
(no real work) Get one friend to sort the first half. Get one friend to sort the second half. 52 88 14 25,31,52,88,98 14,23,30,62,79 31 98 25 30 23 62 79 Merge Sort Split Set into Two
25,31,52,88,98 14,23,30,62,79 14,23,25,30,31,52,62,79,88,98 Merge Sort Merge two sorted lists into one
Merge Sort Time: T(n) = 2T(n/2) + Q(n) = Q(n log n)
Divide and Conquer 52 88 14 31 98 25 30 23 62 79 Quick Sort
≤52 ≤ 52 88 88 14 14 98 31 98 62 25 30 30 31 79 23 23 62 25 79 Quick Sort Partition set into two using randomly chosen pivot
Get one friend to sort the second half. Get one friend to sort the first half. ≤52 ≤ 88 14 98 14,23,25,30,31 62,79,98,88 62 30 31 79 23 25 Quick Sort
Glue pieces together. (No real work) 52 14,23,25,30,31 62,79,98,88 14,23,25,30,31,52,62,79,88,98 Quick Sort
52 88 14 31 98 25 30 23 62 79 Quick Sort Let pivot be the first element in the list? 14 88 98 30 ≤31 ≤ 62 23 25 52 79
14,23,25,30,31,52,62,79,88,98 23,25,30,31,52,62,79,88,98 Quick Sort ≤14 ≤ If the list is already sorted, then the list is worst case unbalanced.
T(n) = 2T(n/2) + Q(n) = Q(n log(n)) Quick Sort Best Time: Worst Time: Expected Time:
T(n) = T(0) + T(n-1) + Q(n) Quick Sort Best Time: T(n) = 2T(n/2) + Q(n) = Q(n log(n)) Worst Time: = Q(n2) Expected Time:
Quick Sort Best Time: T(n) = 2T(n/2) + Q(n) = Q(n log(n)) Worst Time: T(n) = T(0) + T(n-1) + Q(n) = Q(n2) Expected Time: T(n) = T(1/3n) + T(2/3n) + Q(n) = Q(n log(n))
Heap Definition • Completely Balanced Binary Tree • The value of each node • ³ each of the node's children. • Left or right child could be larger. Where can 9 go? Maximum is at root. Where can 1 go? Where can 8 go?
Heap Data Structure Completely Balanced Binary TreeImplemented by an Array
Make Heap Get help from friends
Heapify Where is the maximum? Maximum is at root. ?
Put it in place ? Heapify Find the maximum. Repeat
Heap Heapify Running Time:
Make Heap Get help from friends Running time: T(n) = 2T(n/2) + log(n) = Q(n)
Heap Heaps ?
Heap ?
? Heap
log(n) -i 2log(n) -i i Running Time:
Largest i values are sorted on side. Remaining values are off to side. 5 3 1 4 2 < 6,7,8,9 Insertion Sort Insertion Max is easier to find if a heap.
Largest i values are sorted on side. Remaining values are in a heap. Heap Sort
Heap Data Structure Heap 7 6 Array 8 9 3 4 2 1 5 Heap Array
Largest i values are sorted on side. Remaining values are ? Heap Heap Sort in a heap Put next value where it belongs.
Heap Sort ? ? ? ? ? ? ? Heap
Heap Sort Running Time:
Priority Queues • Maintains dynamic set, S, of elements, each with a key. • Max-priority queue supports: • INSERT(S,x) • MAXIMUM(S) • EXTRACT-MAX(S) • INCREASE-KEY(S,x,k) • Application: Shedule jobs on a shared computer.
Priority Queues cont’d... • MAXIMUM(S): • EXTRACT-MAX(S):
Priority Queues cont’d... • INCREASE-KEY(S,x,k): • INSERT(S,x):
_ _ _ _ _ 5 1 5 3 3 2 _ _ _ _ _ _ A = B = C = Counting Sort • Input: Array A[1,…,n], with all elements in {1,..,k}. • Output: Sorted array B[1,..,n]. • Auxiliary Storage:C[1,..,k].