1 / 101

Sorting & Lower Bounds

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...

francie
Download Presentation

Sorting & Lower Bounds

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Thinking about Algorithms Abstractly Sorting & Lower Bounds Jeff Edmonds York University Lecture5 COSC 3101

  2. 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

  3. Review of Sorting Algorithms

  4. 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

  5. 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 …

  6. 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 …

  7. Selection Sort Q(n2) Average & Worst Time: Insertion Sort Q(n2) Average & Worst Time:

  8. Divide and Conquer 52 88 14 31 98 25 30 23 62 79 Merge Sort

  9. (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

  10. 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

  11. Merge Sort Time: T(n) = 2T(n/2) + Q(n) = Q(n log n)

  12. Divide and Conquer 52 88 14 31 98 25 30 23 62 79 Quick Sort

  13. ≤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

  14. 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

  15. 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

  16. 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

  17. 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.

  18. T(n) = 2T(n/2) + Q(n) = Q(n log(n)) Quick Sort Best Time: Worst Time: Expected Time:

  19. 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:

  20. 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))

  21. Heaps, Heap Sort, &Priority Queues

  22. 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?

  23. Heap Data Structure Completely Balanced Binary TreeImplemented by an Array

  24. Make Heap Get help from friends

  25. Heapify Where is the maximum? Maximum is at root. ?

  26. Put it in place ? Heapify Find the maximum. Repeat

  27. Heap Heapify Running Time:

  28. Make Heap Get help from friends Running time: T(n) = 2T(n/2) + log(n) = Q(n)

  29. Heap Heaps ?

  30. Heap ?

  31. ? Heap

  32. ?

  33. Heap

  34. log(n) -i 2log(n) -i i Running Time:

  35. 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.

  36. Largest i values are sorted on side. Remaining values are in a heap. Heap Sort

  37. Heap Data Structure Heap 7 6 Array 8 9 3 4 2 1 5 Heap Array

  38. Largest i values are sorted on side. Remaining values are ? Heap Heap Sort in a heap Put next value where it belongs.

  39. Heap Sort ? ? ? ? ? ? ? Heap

  40. Heap Sort

  41. Heap Sort Running Time:

  42. 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.

  43. Priority Queues cont’d... • MAXIMUM(S): • EXTRACT-MAX(S):

  44. Priority Queues cont’d... • INCREASE-KEY(S,x,k): • INSERT(S,x):

  45. Other Sorting Algorithms:Counting SortRadix SortBucket Sort

  46. _ _ _ _ _ 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].

More Related