1 / 76

Lecture 2 Sorting

Lecture 2 Sorting. Sorting Problem. e.g.,. Insertion Sort, Merge Sort. Efficiency. Running time from receiving the input to producing the output. Running time. Insertion Sort. Merge Sort. Quiz Sample. Is array a data structure?. Is array a data structure?. No!

gloriap
Download Presentation

Lecture 2 Sorting

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

  2. Sorting Problem e.g., Insertion Sort, Merge Sort

  3. Efficiency • Running time from receiving the input to producing the output. Running time Insertion Sort Merge Sort

  4. Quiz Sample • Is array a data structure?

  5. Is array a data structure? • No! • A data structure is a standard part in construction of algorithms. • What data structures do you know on array?

  6. Is array a data structure? • No! • A data structure is a standard part in construction of algorithms. • What data structures do you know on array? • Stack, queue, list, …, heap.

  7. Heapsort • Heap, a data structure • Max-Heapify procedure • Building a heap • Heapsort

  8. A Data Structure Heap • A heap is an array object that can be viewed as a nearly complete binary tree. 1 6 2 3 5 3 6 5 3 2 4 1 4 5 6 2 4 1

  9. Max-Heap

  10. Min-Heap

  11. Max-Heapify • Max-Heapify(A,i) is a subroutine. • When it is called, two subtrees rooted at Left(i) and Right(i) are max-heaps, but A[i] may not satisfy the max-heap property. • Max-Heapify(A,i) makes the subtree rooted at A[i] become a max-heap by letting A[i] “float down”.

  12. 4 14 7 4 14 7 2 8 1 2 8 1 14 8 7 2 1 4

  13. Running time

  14. Building a Heap e.g., 4, 1, 3, 2, 16, 9, 10, 14, 8, 7.

  15. 4 1 3 10 9 2 16 8 7 14 Proof.

  16. 4 1 3 10 9 2 16 8 7 7 14

  17. 4 1 3 10 9 2 16 8 7 14

  18. 4 1 3 10 9 2 16 8 7 14

  19. Building a Max-Heap e.g., 4, 1, 3, 2, 16, 9, 10, 14, 8, 7.

  20. 4 1 3 10 9 2 16 8 7 14

  21. 4 1 3 10 9 2 16 8 7 14

  22. 4 1 3 10 9 16 14 2 8 7

  23. 4 1 3 10 9 14 16 2 8 7

  24. 4 1 10 3 9 14 16 2 8 7

  25. 4 1 10 3 9 14 16 2 8 7

  26. 4 16 10 3 9 14 1 2 8 7

  27. 4 16 10 3 9 14 7 2 8 1

  28. 16 4 10 3 9 14 7 2 8 1

  29. 16 14 10 3 9 4 7 2 8 1

  30. 16 14 10 3 9 7 8 2 4 1

  31. Analysis

  32. 16 14 10 3 9 7 8 2 4 1

  33. Running time

  34. Heapsort

  35. Input: 4, 1, 3, 2, 16, 9, 10, 14, 8, 7. Build a max-heap 16 14 10 3 9 7 8 2 4 1 16, 14, 10, 8, 7, 9, 3, 2, 4, 1.

  36. 1 14 10 3 9 7 8 2 4 16

  37. 1 14 10 3 9 7 8 2 4 16 1, 14, 10, 8, 7, 9, 3, 2, 4, 16.

  38. 14 8 10 3 9 7 4 2 1 16

  39. 14 8 10 3 9 7 4 2 1 16 14, 8, 10, 4, 7, 9, 3, 2, 1, 16.

  40. 1 8 10 3 9 7 4 2 14 16

  41. 1 8 10 3 9 7 4 2 14 16 1, 8, 10, 4, 7, 9, 3, 2, 14, 16.

  42. 10 8 9 3 1 7 4 2 14 16

  43. 10 8 9 3 1 7 4 2 14 16 10, 8, 9, 4, 7, 1, 3, 2, 14, 16.

  44. 2 8 9 3 1 7 4 14 16 10

  45. 2 8 9 3 1 7 4 14 16 10 2, 8, 9, 4, 7, 1, 3, 10, 14, 16.

  46. 9 8 3 2 1 7 4 14 16 10

  47. 9 8 3 2 1 7 4 14 16 10 9, 8, 3, 4, 7, 1, 2, 10, 14, 16.

  48. 2 8 3 9 1 7 4 14 16 10

More Related