Download presentation
Presentation is loading. Please wait.
1
Chapter 7: Sorting Algorithms Heap Sort Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College
2
2 Heap Sort Basic Idea Complexity Example Animation Animation
3
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
4 Improvements Use the same array to 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
5 Improvements When all the elements are deleted and stored in the same array following the above method, the elements will be there in reversed order. What is the remedy for this? Store the elements in the binary heap tree in reverse order of priority - then at the end the elements in the array will be in correct order.
6
6 Complexity Sorts in O(NlogN) time by performing N times deleteMax operations. - Each deleteMax operation takes log N running time. - N times performing deleteMax NlogN running time Used for general purpose sorting, guarantees O(N logN)
7
7 Example 15191071716 1. Consider the values of the elements as priorities and build the heap tree. 2. Start deleteMax operations, storing each deleted element at the end of the heap array.
8
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
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. 151971716 10 Result: 15191671710 holechild
10
10 Build the Heap Next we compare position 2 with its children. 151671710 19 holechild1child2 19 is greater than 7 and 17, and we continue with position 1 15191671710
11
11 Build the Heap Percolate down the hole at position 1 191671710 15 The hole at position 1 is percolated down to position 2 -the greater child. 191671710 15
12
12 Build the Heap Percolate down the hole at position 2 191671710 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. 19171671510
13
13 Build the Heap 19171671510 19 1716 71510 the heap is built
14
14 Sorting DeleteMax the top element 19 171671519 1716 715 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 10
15
15 Sorting Percolate down the hole 1671519 16 715 10 17
16
16 Sorting Percolate down the hole 1671519 16 7 15 10 17
17
Sorting Fill the hole 1671519 10 16 7 15 1017
18
18 Sorting DeleteMax the top element 17 1671519 16 7 15 17 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
19
19 Sorting Percolate down the hole 1671519 16 7 15 17 10
20
20 Sorting Fill the hole 1671519 10 16 7 15 1710
21
21 Sorting DeleteMax the top element 16 161519 1015 1710 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 7
22
22 Sorting Percolate down the hole 161519 10 15 1710 7
23
23 Sorting Fill the hole 161519 10 7 15 17107
24
24 Sorting DeleteMax the top element 15 161519 7 17 10 7 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
25
25 Sorting Percolate down the hole 161519 7 17 10 7 Since 10 is greater than the children of the hole, It has to be inserted in the hole
26
26 Sorting Fill the hole 161519 7 10 17107
27
27 Sorting DeleteMax the top element 10 16151917 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 10
28
28 Sorting Fill the hole 161519 7 17710 The hole has no children and so it has to be filled.
29
29 Sorted array 16151917710 7 is the last element from the heap, so now the array is sorted
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.