Sorting Algorithms: Heap Sort
Sorting Algorithms: Heap Sort
Heap Sort
Basic Idea Complexity Example Animation
Idea
Store N elements in a binary heap tree.
storing each element deleted from the heap into another array. Copy back the array.
Not very efficient to use two arrays.
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.
4
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.
5
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
Example
15 19 10 7 17 16
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.
7
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
15
19
hole
17
16
child
10
Result:
15 19 16 7 17 10
9
16
child1
17
child2
10
19
16
17
10
16
17
10
16
17
10
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
12
15
10
Sorting
15 19
17
10
16
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
17
7 15
16
14
Sorting
7 15 19
17
17 10
16
16
7 15
15
Sorting
7 19
17 15 7
16
17 10
15
16
16
Sorting
16 7 10 19
17 15 7 10
17
17
15
16
Sorting
17 19
15
10
16
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 7
16
18
Sorting
7 17 19 16
15 7
19
16
10
15
Sorting
10 7 17 19 16
15 7
20
16
15
10
Sorting
17 19
15
7
10
16
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
21
Sorting
16 17 19
15 10
15
7
10
22
Sorting
10 16 17 19
15
15
10
23
Sorting
17 19
7
10
15
16
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
24
Sorting
16 17 19
7
10
15
Since 10 is greater than the children of the hole, It has to be inserted in the hole
25
Sorting
15 16 17 19
10
10
26
Sorting
17 19
10
7
15
16
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
27
Sorting
15 16 17 19
7
10
28
Sorted array
7 10 15 16 17 19
29