1 / 29

Chapter 7: Sor ting A lgorithms

Mark Allen Weiss: Data Structures and Algorithm Analysis in Java. Chapter 7: Sor ting A lgorithms. Heap Sort. Lydia Sinapova, Simpson College. Heap Sort. Basic Idea Complexity Example Animation. Idea. Store N elements in a binary heap tree.

ivan
Download Presentation

Chapter 7: Sor ting A lgorithms

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. Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Chapter 7: Sorting Algorithms Heap Sort Lydia Sinapova, Simpson College

  2. Heap Sort • Basic Idea • Complexity • Example • Animation

  3. Idea • Store N elements in a binary heap tree. • Perform delete_Min operation N times, storing each element deleted from the heap into another array. • Copy back the array. • Not very efficient to use two arrays. • Improvement – use one array for the binary heap and the sorted elements

  4. Improvements Use the same arrayto store the deleted elements instead of using another array After each deletion we get a vacant position in the array - the last cell. There we store the deleted element, which becomes part of the sorted sequence.

  5. Improvements When all the elements are deleted and stored in the same array following the above method, the elements will be there inreversed order. What is the remedy for this? Store the elements in the binary heap tree inreverse order of priority- then at the end the elements in the array will be in correct order.

  6. Complexity Sorts inO(NlogN)time by performing N timesdeleteMax operations. -      Each deleteMax operation takeslog Nrunning time. -      N times performing deleteMaxNlogNrunning time Used for general purpose sorting, guarantees O(N logN)

  7. 15 19 10 7 17 16 Example 1. Consider the values of the elements as priorities and build the heap tree. 2. Start deleteMaxoperations, storing each deleted element at the end of the heap array.

  8. Example (cont) Note that we use only one array , treating its parts differently: when sorting, part of the array will be the heap, and the rest part - the sorted array

  9. Build the Heap We start with the element at position SIZE/2 comparing the item with the children. The hole is percolated down to position 6 and the item is inserted there. hole child 15 19 7 17 16 10 Result: 15 19 16 7 17 10

  10. Build the Heap Next we compare position 2 with its children. hole child1 child2 15 16 7 17 10 19 19 is greater than 7 and 17, and we continue with position 1 15 19 16 7 17 10

  11. Build the Heap Percolate down the hole at position 1 19 16 7 17 10 15 The hole at position1is percolated down to position 2 -the greaterchild. 19 16 7 17 10 15

  12. Build the Heap Percolate down the hole at position 2 19 16 7 17 10 15 One of the children of the hole at position 2 - item 17, is greater than 15. So we percolate the hole to position 5. 19 17 16 7 15 10

  13. 19 17 16 7 15 10 Build the Heap 19 17 16 7 15 10 the heap is built

  14. 17 16 7 15 Sorting DeleteMax the top element 19 17 16 7 15 19 10 Store the last heap element (10) in a temporary place. Move the DeletedMax element (19) to the place where the last heap element was - the current available position in the sorted portion of the array. A hole is created at the top

  15. 16 7 15 Sorting Percolate down the hole 17 16 7 15 19 17 10

  16. Sorting Percolate down the hole 17 15 16 7 19 17 10 15 16 7

  17. Sorting Fill the hole 17 15 16 7 10 19 17 15 16 10 7

  18. Sorting DeleteMax the top element 17 15 16 7 17 19 10 Store the last heap element (10) in a temporary place. Move the DeletedMax element (17) to the place where the last heap element was - the current available position in the sorted portion of the array. A hole is created at the top 15 16 7

  19. Sorting Percolate down the hole 16 15 7 17 19 10 16 15 7

  20. Sorting Fill the hole 16 15 10 7 17 19 16 15 10 7

  21. Sorting DeleteMax the top element 16 15 10 16 17 19 7 Store the last heap element (7) in a temporary place. Move the DeletedMax element (16) to the place where the last heap element was - the current available position in the sorted portion of the array. A hole is created at the top 15 10

  22. Sorting Percolate down the hole 15 10 16 17 19 7 15 10

  23. Sorting Fill the hole 15 7 10 16 17 19 15 7 10

  24. Sorting DeleteMax the top element 15 7 15 16 17 19 10 Store the last heap element (10) in a temporary place. Move the DeletedMax element (15) to the place where the last heap element was - the current available position in the sorted portion of the array. A hole is created at the top 7

  25. Sorting Percolate down the hole 7 15 16 17 19 10 7 Since 10 is greater than the children of the hole, It has to be inserted in the hole

  26. Sorting Fill the hole 10 7 15 16 17 19 10 7

  27. Sorting DeleteMax the top element 10 10 15 16 17 19 7 Store the last heap element (7) in a temporary place. Move the DeletedMax element (10) to the place where the last heap element was - the current available position in the sorted portion of the array. A hole is created at the top

  28. Sorting Fill the hole 7 10 15 16 17 19 7 The hole has no children and so it has to be filled.

  29. Sorted array 7 10 15 16 17 19 7 is the last element from the heap, so now the array is sorted

More Related