Heap Sort
Algorithm
Definitions of
heap:
Aheap is a data structure that stores a collection of
objects (with keys), and has the following
properties:
Complete Binary tree
Heap Order
The heap sort algorithm has
two major steps :
i. The first major step involves transforming the complete
tree intoa heap.
ii. The second major step is to perform the actual sort by
extracting the largest or lowerst element from the root
and transforming the remaining tree into a heap.
Types of
heap
Max Heap
Min Heap
Max Heap Example
19 12 16 1 4 7
ArrayA
19
12 16
4
1 7
Min heap example
1 4 16 7 12 19
ArrayA
1
4 16
12
7 19
1-Max heap :
max-heap Definition:
is a complete binary tree in which the value in
each internal node is greater than or equal to
the values in the children of that node.
Max-heap property:
 The key of a node is ≥ than the keys of its children.
Max heap Operation
Aheap can be stored as an
array A.
Root of treeis A[1]
Left child of A[i] = A[2i]
Right child of A[i] = A[2i + 1]
Parent of A[i] = A[ i/2 ]
8
Example Explaining :
A
4 1 3 2 16 9 10 14 8 7
2
1
3
3
2
1
9
3
10
2
1
3
3
1 1 1
4 4 4
2
1
3
3
5 6
16 9
7
10
4 5 6 7 4 5 6 7 4
2 9
16
10 9 10 8
2 9
16
10 9 10 8 14
8 9 10
14 8 7 14 8 7 2 8 7
2
16
3
10
1
16
2
14
3
10
5 6
7 9
7
3
4 5 6 7 4 5 6 7 4
14 9
16
10 9 3 8
14 9
7
10 9 3 8 8
8 9 10
2 8 7 2 8 1 2 4 1
i = 5 i = 4 i = 3
i = 2
1
4
i = 1
1
4
Build Max-heap Heapify() Example
16
4 10
14 7 9 3
2 8 1
16 4 10 14 7 9 3 2 8 1
10
1
1
/20/2017
A=
Heapify() Example
16
4 10
14 7 9 3
2 8 1
A= 16
11
1
1
/20/2017
4 10 14 7 9 3 2 8 1
Heapify()
Example 16
4 10
14 7 9 3
2 8 1
A= 16 4 10 14 7 9 3 2 8 1
12
1
1
/20/2017
Heapify() Example
16
14 10
4 7 9 3
2 8 1
16 14 10 4 7 9 3 2 8 1
13
1
1
/20/2017
A=
Heapify()
Example 16
14 10
4 7 9 3
2 8 1
A= 16 14 10 4 7 9 3 2 8 1
David Luebke
14
1
1
/20/2017
Heapify() Example
16
14 10
4 7 9 3
2 8 1
A= 16 14 10 4 7 9 3 2 8 1
15
1
1
/20/2017
Heapify()
Example 16
14 10
8 7 9 3
2 4 1
16 14 10 8 7 9 3 2 4 1
16
1
1
/20/2017
A=
Heapify() Example
16
14 10
8 7 9 3
2 4 1
A= 16 14 10 8 7 9 3 2 4 1
17
1
1
/20/2017
Heapify()
Example 16
14 10
8 7 9 3
2 4 1
16 14 10 8 7 9 3 2 4 1
18
1
1
/20/2017
A=
Heap-Sort
sorting strategy:
1. Build Max Heap from unordered array;
2. Find maximum elementA[1];
3. Swap elements A[n] andA[1]:
now max element is at the end of the array! .
4. Discard node n from heap
(by decrementing heap-sizevariable).
5. New root may violate max heap property, but its
children are max heaps. Run max_heapify to fix this.
6. Go to Step 2 unless heap is empty.
HeapSort() Example
10
A= {16,14,10,8, 7, 9, 3, 2, 4, 1
}
1
16
20
2
14
3
4
8
5
7
6
9
7
3
8
2
9
4
10
1
HeapSort() Example
10
A= {14,8, 10,4, 7, 9, 3, 2, 1,16}
1
14
21
2
8
3
4
4
5
7
6
9
7
3
8
2
9
1 16
i =10
HeapSort() Example
9
A= {10,8, 9, 4, 7, 1,3, 2, 14,16}
1
10
22
2
8
3
4
4
5
7
6
1
7
3
8
2 16
10
14
i =9
HeapSort() Example
3
A= {9, 8, 3, 4, 7, 1,2, 10,14, 16}
1
9
23
2
8
3
4
4
5
7
6
1
7
2
14
9
16
10
10
i =8
HeapSort() Example
3
A= {8, 7, 3, 4, 2, 1,9, 10,14, 16}
1
8
24
2
7
3
4
4
5
2
6
1
10
8
14
9
16
10
9
i =7
HeapSort() Example
3
A= {7,4, 3, 1,2, 8, 9, 10,14, 16}
1
7
25
2
4
3
4
1
5
2 9
7
10
8
14
9
16
10
8
i =6
HeapSort() Example
3
A= {4, 2, 3, 1,7, 8, 9, 10,14, 16}
1
4
26
2
2
3
4
1 8
6
9
7
10
8
14
9
16
10
i =5
7
HeapSort() Example
1
4
A= {3,2, 1,4, 7, 8, 9, 10,14,16}
1
3
27
2
2
3
7
5
8
6
9
7
10
8
14
9
16
10
i =4
HeapSort() Example
A= {2,1,3, 4, 7, 8, 9, 10,14, 16}
4
1
2
28
2
1
4
7
5
8
6
9
7
10
8
14
9
16
10
i =3
3
HeapSort() Example
A= {1,2, 3, 4, 7, 8, 9, 10,14,16}>>orederd
4
1
1
29
3
3
4
7
5
8
6
9
7
10
8
14
9
16
10
i =2
2
Heap Sort pseducode
Heapsort(A asarray)
BuildHeap(A)
for i = n to 1
swap(A[1], A[i])
n = n - 1
Heapify(A, 1)
BuildHeap(A as array)
n = elements_in(A)
for i = floor(n/2) to1
Heapify(A,i)
Heapify(A asarray, i as int)
left =2i
right =2i+1
if (left <= n) and (A[left] >A[i])
max =left
else
max =i
if (right<=n) and (A[right] > A[max])
max =right
if (max != i)
swap(A[i], A[max])
Heapify(A, max)
2-Min heap :
min-heap Definition:
is a complete binary tree in which the value in
each internal node is lower than or equal to the
values in the children of that node.
Min-heap property:
 The key of a node is <= than the keys of its
children.
Min heap Operation
Aheap can be stored as an array A.
Root of treeis A[0]
Left child of A[i] = A[2i+1]
Right child of A[i] = A[2i + 2]
Parent of A[i] = A[( i/2)-1]
33
Min Heap
19
12 16
4
1 7
19 12 16 1 4 7
ArrayA
Min Heap phase 1
19
1 16
4
12 7
Min Heap phase 1
1
19 7
4
12 16
Min Heap phase 1
1
4 7
19
12 16
1,
Min Heap phase 2
16
4 7
19
12
Min Heap phase 2
4
16 7
19
12
Min Heap phase 2
4
12 7
19
16
1,4
Min Heap phase 3
19
12 7
16
Min Heap phase 3
7
12 19
16
1,4,7
Min Heap phase 4
16
12 19
Min Heap phase 4
12
16 19
1,4,7,12
Min Heap phase 5
19
16
Min Heap phase 5
16
19
1,4,7,12,16
Min Heap phase 7
19
1,4,7,12,16,19
Min heap final tree
1 4 7 12 16 19
ArrayA
1
4 7
16
12 19
Insertion
Algorithm
1. Add the new element to the next available position at the
lowest level
2. Restore the max-heap property if violated
General strategy is percolate up (or bubble up): if
the parent of the element is smaller than the
element, then interchange the parent and child.
OR
Restore the min-heap property if violated
General strategy is percolate up (or bubble up): if
the parent of the element is larger than the element,
then interchange the parent and child.
19
12 16
4
1 7
19
12 16
4
1 7 17
19
12 17
1 4 7 16
Insert 17
swap
Percolate up to maintain the heap
property
Conclusion
The primary advantage of the heap sort is its
efficiency. The execution time efficiency of the
heap sort is O(n log n).
The memory efficiency of the heap sort, unlike
the other n log n sorts, is constant, O(1), because
the heap sort algorithm is not recursive.

Heap_Sort1.pptx

  • 1.
  • 2.
    Definitions of heap: Aheap isa data structure that stores a collection of objects (with keys), and has the following properties: Complete Binary tree Heap Order
  • 3.
    The heap sortalgorithm has two major steps : i. The first major step involves transforming the complete tree intoa heap. ii. The second major step is to perform the actual sort by extracting the largest or lowerst element from the root and transforming the remaining tree into a heap.
  • 4.
  • 5.
    Max Heap Example 1912 16 1 4 7 ArrayA 19 12 16 4 1 7
  • 6.
    Min heap example 14 16 7 12 19 ArrayA 1 4 16 12 7 19
  • 7.
    1-Max heap : max-heapDefinition: is a complete binary tree in which the value in each internal node is greater than or equal to the values in the children of that node. Max-heap property:  The key of a node is ≥ than the keys of its children.
  • 8.
    Max heap Operation Aheapcan be stored as an array A. Root of treeis A[1] Left child of A[i] = A[2i] Right child of A[i] = A[2i + 1] Parent of A[i] = A[ i/2 ] 8
  • 9.
    Example Explaining : A 41 3 2 16 9 10 14 8 7 2 1 3 3 2 1 9 3 10 2 1 3 3 1 1 1 4 4 4 2 1 3 3 5 6 16 9 7 10 4 5 6 7 4 5 6 7 4 2 9 16 10 9 10 8 2 9 16 10 9 10 8 14 8 9 10 14 8 7 14 8 7 2 8 7 2 16 3 10 1 16 2 14 3 10 5 6 7 9 7 3 4 5 6 7 4 5 6 7 4 14 9 16 10 9 3 8 14 9 7 10 9 3 8 8 8 9 10 2 8 7 2 8 1 2 4 1 i = 5 i = 4 i = 3 i = 2 1 4 i = 1 1 4
  • 10.
    Build Max-heap Heapify()Example 16 4 10 14 7 9 3 2 8 1 16 4 10 14 7 9 3 2 8 1 10 1 1 /20/2017 A=
  • 11.
    Heapify() Example 16 4 10 147 9 3 2 8 1 A= 16 11 1 1 /20/2017 4 10 14 7 9 3 2 8 1
  • 12.
    Heapify() Example 16 4 10 147 9 3 2 8 1 A= 16 4 10 14 7 9 3 2 8 1 12 1 1 /20/2017
  • 13.
    Heapify() Example 16 14 10 47 9 3 2 8 1 16 14 10 4 7 9 3 2 8 1 13 1 1 /20/2017 A=
  • 14.
    Heapify() Example 16 14 10 47 9 3 2 8 1 A= 16 14 10 4 7 9 3 2 8 1 David Luebke 14 1 1 /20/2017
  • 15.
    Heapify() Example 16 14 10 47 9 3 2 8 1 A= 16 14 10 4 7 9 3 2 8 1 15 1 1 /20/2017
  • 16.
    Heapify() Example 16 14 10 87 9 3 2 4 1 16 14 10 8 7 9 3 2 4 1 16 1 1 /20/2017 A=
  • 17.
    Heapify() Example 16 14 10 87 9 3 2 4 1 A= 16 14 10 8 7 9 3 2 4 1 17 1 1 /20/2017
  • 18.
    Heapify() Example 16 14 10 87 9 3 2 4 1 16 14 10 8 7 9 3 2 4 1 18 1 1 /20/2017 A=
  • 19.
    Heap-Sort sorting strategy: 1. BuildMax Heap from unordered array; 2. Find maximum elementA[1]; 3. Swap elements A[n] andA[1]: now max element is at the end of the array! . 4. Discard node n from heap (by decrementing heap-sizevariable). 5. New root may violate max heap property, but its children are max heaps. Run max_heapify to fix this. 6. Go to Step 2 unless heap is empty.
  • 20.
    HeapSort() Example 10 A= {16,14,10,8,7, 9, 3, 2, 4, 1 } 1 16 20 2 14 3 4 8 5 7 6 9 7 3 8 2 9 4 10 1
  • 21.
    HeapSort() Example 10 A= {14,8,10,4, 7, 9, 3, 2, 1,16} 1 14 21 2 8 3 4 4 5 7 6 9 7 3 8 2 9 1 16 i =10
  • 22.
    HeapSort() Example 9 A= {10,8,9, 4, 7, 1,3, 2, 14,16} 1 10 22 2 8 3 4 4 5 7 6 1 7 3 8 2 16 10 14 i =9
  • 23.
    HeapSort() Example 3 A= {9,8, 3, 4, 7, 1,2, 10,14, 16} 1 9 23 2 8 3 4 4 5 7 6 1 7 2 14 9 16 10 10 i =8
  • 24.
    HeapSort() Example 3 A= {8,7, 3, 4, 2, 1,9, 10,14, 16} 1 8 24 2 7 3 4 4 5 2 6 1 10 8 14 9 16 10 9 i =7
  • 25.
    HeapSort() Example 3 A= {7,4,3, 1,2, 8, 9, 10,14, 16} 1 7 25 2 4 3 4 1 5 2 9 7 10 8 14 9 16 10 8 i =6
  • 26.
    HeapSort() Example 3 A= {4,2, 3, 1,7, 8, 9, 10,14, 16} 1 4 26 2 2 3 4 1 8 6 9 7 10 8 14 9 16 10 i =5 7
  • 27.
    HeapSort() Example 1 4 A= {3,2,1,4, 7, 8, 9, 10,14,16} 1 3 27 2 2 3 7 5 8 6 9 7 10 8 14 9 16 10 i =4
  • 28.
    HeapSort() Example A= {2,1,3,4, 7, 8, 9, 10,14, 16} 4 1 2 28 2 1 4 7 5 8 6 9 7 10 8 14 9 16 10 i =3 3
  • 29.
    HeapSort() Example A= {1,2,3, 4, 7, 8, 9, 10,14,16}>>orederd 4 1 1 29 3 3 4 7 5 8 6 9 7 10 8 14 9 16 10 i =2 2
  • 30.
    Heap Sort pseducode Heapsort(Aasarray) BuildHeap(A) for i = n to 1 swap(A[1], A[i]) n = n - 1 Heapify(A, 1) BuildHeap(A as array) n = elements_in(A) for i = floor(n/2) to1 Heapify(A,i)
  • 31.
    Heapify(A asarray, ias int) left =2i right =2i+1 if (left <= n) and (A[left] >A[i]) max =left else max =i if (right<=n) and (A[right] > A[max]) max =right if (max != i) swap(A[i], A[max]) Heapify(A, max)
  • 32.
    2-Min heap : min-heapDefinition: is a complete binary tree in which the value in each internal node is lower than or equal to the values in the children of that node. Min-heap property:  The key of a node is <= than the keys of its children.
  • 33.
    Min heap Operation Aheapcan be stored as an array A. Root of treeis A[0] Left child of A[i] = A[2i+1] Right child of A[i] = A[2i + 2] Parent of A[i] = A[( i/2)-1] 33
  • 34.
    Min Heap 19 12 16 4 17 19 12 16 1 4 7 ArrayA
  • 35.
    Min Heap phase1 19 1 16 4 12 7
  • 36.
    Min Heap phase1 1 19 7 4 12 16
  • 37.
    Min Heap phase1 1 4 7 19 12 16 1,
  • 38.
    Min Heap phase2 16 4 7 19 12
  • 39.
    Min Heap phase2 4 16 7 19 12
  • 40.
    Min Heap phase2 4 12 7 19 16 1,4
  • 41.
    Min Heap phase3 19 12 7 16
  • 42.
    Min Heap phase3 7 12 19 16 1,4,7
  • 43.
    Min Heap phase4 16 12 19
  • 44.
    Min Heap phase4 12 16 19 1,4,7,12
  • 45.
  • 46.
    Min Heap phase5 16 19 1,4,7,12,16
  • 47.
    Min Heap phase7 19 1,4,7,12,16,19
  • 48.
    Min heap finaltree 1 4 7 12 16 19 ArrayA 1 4 7 16 12 19
  • 49.
    Insertion Algorithm 1. Add thenew element to the next available position at the lowest level 2. Restore the max-heap property if violated General strategy is percolate up (or bubble up): if the parent of the element is smaller than the element, then interchange the parent and child. OR Restore the min-heap property if violated General strategy is percolate up (or bubble up): if the parent of the element is larger than the element, then interchange the parent and child.
  • 50.
    19 12 16 4 1 7 19 1216 4 1 7 17 19 12 17 1 4 7 16 Insert 17 swap Percolate up to maintain the heap property
  • 51.
    Conclusion The primary advantageof the heap sort is its efficiency. The execution time efficiency of the heap sort is O(n log n). The memory efficiency of the heap sort, unlike the other n log n sorts, is constant, O(1), because the heap sort algorithm is not recursive.