Heap Sort
Dr. Ra’Fat A. AL-msie’deen
Chapter 3
Mutah University
Faculty of IT, Department of Software Engineering
Outlines: Heap Sort
• Input: One-Dimension Array.
• Advantages of Insertion Sort and Merge Sort
• Heap Sort:
 The Heap Property.
 Heapify Function “MAX-HEAPIFY (A, i)”
 Build Heap Function “BUILD-MAX-HEAP(A)”
 Heap Sort Function.
• Max-Priority Queues (Basic Operations):
 Maximum
 Extract-Max
 Increase Key
 Insert Key
2
1D Array
• 1-dimensional array x = [a, y, f, k]
• x[1] = a; x[2] = y; x[3] = f; x[4] = k
3
a y f kMemory
start
Sorting Revisited
• So far we’ve talked about two algorithms to sort an array of
numbers:
o What is the advantage of merge sort?
 Answer: good worst-case running time O(n lg n).
 Conceptually easy, Divide-and-Conquer.
o What is the advantage of insertion sort?
 Answer: sorts in place: only a constant number of array
elements are stored outside the input array at any time.
 Easy to code, When array “nearly sorted”, runs fast in
practice.
 Avg. case=worst case=n2
• Next on the agenda: Heapsort.
 Combines advantages of both previous algorithms.
4
Heaps
• A heap can be seen as a complete binary tree.
• In practice, heaps are usually implemented as arrays.
• An array A that represent a heap is an object with two
attributes:
 A[1 .. length[A]].
 length[A]: # of elements in the array.
 heap-size[A]: # of elements in the heap stored within
array A, where heap-size[A] ≤ length[A].
 No element past A[heap-size[A]] is an element of the
heap.
5
1 5 6 77 8 99 14 66 72 101A =
Heaps (Cont.)
• For example, heap-size of the following heap = 10.
• Also, length[A] = 10.
6
Floors & Ceilings
• For any real number x, we denote the greatest
integer less than or equal to x by
o read “the floor of x”
• For any real number x, we denote the least integer
greater than or equal to x by
o read “the ceiling of x”
• For any integer n,
7
 x
 x
    nnn  2/2/
Referencing Heap Elements
• The root node is A[1].
• Node i is A[i].
• Parent(i) == > return i/2
• Left(i) == > return 2i
• Right(i) == > return 2i + 1
• Level 0 (2, 4, 1).
• Level 1 (8, 7, 9, 3).
• Level 2 (15, 10).
• Level 3 (16).
PARENT (i)
1 return i/2
LEFT (i)
1 return 2i
RIGHT (i)
1 return 2i + 1
The Heap Property
• Heaps also satisfy the heap property:
o A[Parent(i)] ≥ A[i] for all nodes i > 1
o In other words, the value of a node is at most the value of
its parent.
 The largest value in a heap is at its root (A[1]).
 and subtrees rooted at a specific node contain values
no larger than that node’s value.
9
Heap Operations: MAX-HEAPIFY()
• Heapify(): maintain the heap property.
• Given: a node i in the heap with children L and R.
• two subtrees rooted at L and R, assumed to be heaps.
• Problem: The subtree rooted at i may violate the heap
property (How?)
 A[i] may be smaller than its children value
• Action: let the value of the parent node “float down” so
subtree at i satisfies the heap property.
 If A[i] < A[L] or A[i] < A[R], swap A[i] with the largest of A[L]
and A[R].
 Recurse on that subtree.
10
Heap Operations: MAX-HEAPIFY()
MAX-HEAPIFY (A, i)
1 l = LEFT(i)
2 r = RIGHT(i)
3 if l ≤ A.heap-size and A[l] > A[i]
4 largest = l
5 else largest = i
6 if r ≤ A.heap-size and A[r] > A[largest]
7 largest = r
8 if largest ≠ i
9 exchange A[i] with A[largest]
10 MAX-HEAPIFY (A, largest)
11
Heap Operations: MAX-HEAPIFY()
 Heapify (A, 2) Example
 A.heap-size = 10.
 The max-heap property is restored for
node 2 in (b) by exchanging A[2] with A[4].
 The recursive call MAX-HEAPIFY (A, 4).
 Swapping A[4] with A[9].
 Heapify (A, 9)
16 4 10 14 7 9 3 2 8 1
16 14 10 8 7 9 3 2 4 1
Heap Height
• Definitions:
 The height of a node in the tree = the number of edges on
the longest downward path to a leaf.
13
# of nodes in each level
• Fact: an n-element heap has at most 2h-k nodes of
level k, where h is the height of the tree.
 for k = h (root level) == > 2h-h = 20 = 1.
 .
 .
 .
 .
 for k = 0 (leaves level) == > 2h-0 = 2h
14
Heap Operations: BUILD-MAX-HEAP()
• We can build a heap in a bottom-up manner by running
Heapify() on successive subarrays.
// given an unsorted array A, make A a heap
BUILD-MAX-HEAP (A)
1 A.heap-size = A.length
2 for i = [A.length/2] downto 1
3 MAX-HEAPIFY (A, i)
15
BUILD-MAX-HEAP() - Example
• Example: Building a max-heap from the following unsorted
array results in the first heap example. (Work through
example: A = {4, 1, 3, 2, 16, 9, 10, 14, 8, 7})
 i starts off as 5. (n=10, n/2=5)
 MAX-HEAPIFY is applied to subtrees rooted at nodes (in
order): 16, 2, 3, 1, 4.
 A = {16, 14, 10, 8, 7, 9, 3, 2, 4, 1}
BUILD-MAX-HEAP() - Example
17
BUILD-MAX-HEAP() - Example
18
Heapsort
• Given BuildHeap(), an in-place sorting algorithm is easily
constructed:
 Maximum element is at A[1].
 Discard by swapping with element at A[n].
o Decrement heap_size[A].
o A[n] now contains correct value.
 Restore heap property at A[1] by calling Heapify().
 Repeat, always swapping A[1] for A[heap_size(A)].
19
Heapsort
HEAPSORT(A)
1 BUILD-MAX-HEAP(A)
2 for i = A.length downto 2
3 exchange A[1] with A[i]
4 A.heap-size = A.heap-size - 1
5 MAX-HEAPIFY(A, 1)
20
HEAPSORT( ) - Example
• The heapsort algorithm starts by using BUILD-MAX-HEAP to
build a max-heap on the input array A[1 … n], where n =
A.length.
• A = {16, 14, 10, 8, 7, 9, 3, 2, 4, 1}
21
The operation of HEAPSORT. (a) The max-heap data structure just after
BUILD-MAX-HEAP has built it in line 1.
HEAPSORT( ) - Example
22
i= 10 i = 9
 Given an input array, the heapsort algorithm acts as follows:
o Builds a max-heap from the array.
o Starting with the root (the maximum element), the algorithm places the
maximum element into the correct place in the array by swapping it with the
element in the last position in the array.
o “Discard” this last node (knowing that it is in its correct place) by decreasing
the heap size, and calling MAX-HEAPIFY on the new (possibly incorrectly-
placed) root.
o Repeat this "discarding" process until only one node (the smallest element)
remains, and therefore is in the correct place in the array.
HEAPSORT( ) - Example
23
A = {7, 4, 3, 1, 2, 8, 9, 10, 14, 16}
i = 6 i = 7
i = 8
i = 9 i = 10
HEAPSORT( ) - Example
24
HEAPSORT( ) – Example (2)
25
Outlines: Heap Sort
• Input: One-Dimension Array.
• Advantages of Insertion Sort and Merge Sort
• Heap Sort:
 The Heap Property.
 Heapify Function “MAX-HEAPIFY (A, i)”
 Build Heap Function “BUILD-MAX-HEAP(A)”
 Heap Sort Function.
• Max-Priority Queues (Basic Operations):
 Maximum
 Extract-Max
 Increase Key
 Insert Key
26
Max-Priority Queues
• A data structure for maintaining a set S of elements, each with
an associated value called a key.
• Applications:
o Scheduling jobs on a shared computer.
o Prioritizing events to be processed based on their
predicted time of occurrence.
o Printer queue.
• Heap can be used to implement a max-priority queue.
27
Max-Priority Queue: Basic Operations
• Maximum(S): → return A[1]
o returns the element of S with the largest key (value).
• Extract-Max(S):
o removes and returns the element of S with the largest
key.
• Increase-Key(S, x, k):
o increases the value of element x’s key to the new value k,
x.value ≤ k.
• Insert(S, x):
o inserts the element x into the set S, (i.e., S → S ∪ {x}).
28
Finding the maximum element
• Getting the maximum element is easy: it’s the
root.
HEAP-MAXIMUM (A)
1 return A[1]
• Time: Θ(1).
29
HEAP-EXTRACT-MAX (A)
• The procedure HEAP-EXTRACT-MAX implements the EXTRACT-
MAX operation.
HEAP-EXTRACT-MAX (A)
1 if A.heap-size < 1 // zero elements
2 error “heap underflow”
3 max = A[1] // max element in first position
4 A[1] = A[A.heap-size]
// value of last position assigned to first position
5 A.heap-size = A.heap-size - 1
6 MAX-HEAPIFY(A, 1)
7 return max
30
Note: lines 4-6 are similar to the for loop body of Heapsort procedure.
HEAP-INCREASE-KEY(A, i, key)
// increase a value (key) in the array
HEAP-INCREASE-KEY(A, i, key)
1 if key < A[i]
2 error “new key is smaller than current key”
3 A[i] = key
4 while i > 1 and A[Parent (i)] < A[i]
5 exchange A[i] with A[Parent (i)]
6 i = Parent (i) // move index up to parent
31
Increase-Key(A, 4, 15) Example
• A = {16, 14, 10, 8, 7, 9, 3, 2, 4, 1}.
32
i=10i=9
Increase-Key(A, 4, 15) Example
• A = {16, 14, 10, 8, 7, 9, 3, 2, 15, 1}.
• The index i increased to 15.
33
Increase-Key(A, 4, 15) Example
• A = {16, 14, 10, 15, 7, 9, 3, 2, 8, 1}.
• After one iteration of the while loop of lines 4-6, the node and
its parent have exchanged keys (values), and the index i
moves up to the parent.
34
i=4
Increase-Key(A, 4, 15) Example
• A = {16, 15, 10, 14, 7, 9, 3, 2, 8, 1}.
• After one more iteration of the while loop.
• The max-heap property now holds and the procedure
terminates.
35
i=2
Increase-Key(A, 4, 15) Example
36
MAX-HEAP-INSERT (A)
// insert a value at the end of the binary tree then move it in the
right position
MAX-HEAP-INSERT(A, key)
1 A.heap-size = A.heap-size + 1
2 A[A.heap-size]= -∞
3 HEAP-INCREASE-KEY(A, A.heap-size, key)
37
Example: Operation of Heap Insert
• Figure x: The operation of HEAP-INSERT. (a) The heap of Figure (a) before
we insert a node with key 15. (b) A new leaf is added to the tree. (c)
Values on the path from the new leaf to the root are copied down until a
place for the key 15 is found. (d) The key 15 is inserted.
38
Heap Sort
Dr. Ra’Fat A. AL-msie’deen
Chapter 3
Mutah University
Faculty of IT, Department of Software Engineering

Algorithms - "heap sort"

  • 1.
    Heap Sort Dr. Ra’FatA. AL-msie’deen Chapter 3 Mutah University Faculty of IT, Department of Software Engineering
  • 2.
    Outlines: Heap Sort •Input: One-Dimension Array. • Advantages of Insertion Sort and Merge Sort • Heap Sort:  The Heap Property.  Heapify Function “MAX-HEAPIFY (A, i)”  Build Heap Function “BUILD-MAX-HEAP(A)”  Heap Sort Function. • Max-Priority Queues (Basic Operations):  Maximum  Extract-Max  Increase Key  Insert Key 2
  • 3.
    1D Array • 1-dimensionalarray x = [a, y, f, k] • x[1] = a; x[2] = y; x[3] = f; x[4] = k 3 a y f kMemory start
  • 4.
    Sorting Revisited • Sofar we’ve talked about two algorithms to sort an array of numbers: o What is the advantage of merge sort?  Answer: good worst-case running time O(n lg n).  Conceptually easy, Divide-and-Conquer. o What is the advantage of insertion sort?  Answer: sorts in place: only a constant number of array elements are stored outside the input array at any time.  Easy to code, When array “nearly sorted”, runs fast in practice.  Avg. case=worst case=n2 • Next on the agenda: Heapsort.  Combines advantages of both previous algorithms. 4
  • 5.
    Heaps • A heapcan be seen as a complete binary tree. • In practice, heaps are usually implemented as arrays. • An array A that represent a heap is an object with two attributes:  A[1 .. length[A]].  length[A]: # of elements in the array.  heap-size[A]: # of elements in the heap stored within array A, where heap-size[A] ≤ length[A].  No element past A[heap-size[A]] is an element of the heap. 5 1 5 6 77 8 99 14 66 72 101A =
  • 6.
    Heaps (Cont.) • Forexample, heap-size of the following heap = 10. • Also, length[A] = 10. 6
  • 7.
    Floors & Ceilings •For any real number x, we denote the greatest integer less than or equal to x by o read “the floor of x” • For any real number x, we denote the least integer greater than or equal to x by o read “the ceiling of x” • For any integer n, 7  x  x     nnn  2/2/
  • 8.
    Referencing Heap Elements •The root node is A[1]. • Node i is A[i]. • Parent(i) == > return i/2 • Left(i) == > return 2i • Right(i) == > return 2i + 1 • Level 0 (2, 4, 1). • Level 1 (8, 7, 9, 3). • Level 2 (15, 10). • Level 3 (16). PARENT (i) 1 return i/2 LEFT (i) 1 return 2i RIGHT (i) 1 return 2i + 1
  • 9.
    The Heap Property •Heaps also satisfy the heap property: o A[Parent(i)] ≥ A[i] for all nodes i > 1 o In other words, the value of a node is at most the value of its parent.  The largest value in a heap is at its root (A[1]).  and subtrees rooted at a specific node contain values no larger than that node’s value. 9
  • 10.
    Heap Operations: MAX-HEAPIFY() •Heapify(): maintain the heap property. • Given: a node i in the heap with children L and R. • two subtrees rooted at L and R, assumed to be heaps. • Problem: The subtree rooted at i may violate the heap property (How?)  A[i] may be smaller than its children value • Action: let the value of the parent node “float down” so subtree at i satisfies the heap property.  If A[i] < A[L] or A[i] < A[R], swap A[i] with the largest of A[L] and A[R].  Recurse on that subtree. 10
  • 11.
    Heap Operations: MAX-HEAPIFY() MAX-HEAPIFY(A, i) 1 l = LEFT(i) 2 r = RIGHT(i) 3 if l ≤ A.heap-size and A[l] > A[i] 4 largest = l 5 else largest = i 6 if r ≤ A.heap-size and A[r] > A[largest] 7 largest = r 8 if largest ≠ i 9 exchange A[i] with A[largest] 10 MAX-HEAPIFY (A, largest) 11
  • 12.
    Heap Operations: MAX-HEAPIFY() Heapify (A, 2) Example  A.heap-size = 10.  The max-heap property is restored for node 2 in (b) by exchanging A[2] with A[4].  The recursive call MAX-HEAPIFY (A, 4).  Swapping A[4] with A[9].  Heapify (A, 9) 16 4 10 14 7 9 3 2 8 1 16 14 10 8 7 9 3 2 4 1
  • 13.
    Heap Height • Definitions: The height of a node in the tree = the number of edges on the longest downward path to a leaf. 13
  • 14.
    # of nodesin each level • Fact: an n-element heap has at most 2h-k nodes of level k, where h is the height of the tree.  for k = h (root level) == > 2h-h = 20 = 1.  .  .  .  .  for k = 0 (leaves level) == > 2h-0 = 2h 14
  • 15.
    Heap Operations: BUILD-MAX-HEAP() •We can build a heap in a bottom-up manner by running Heapify() on successive subarrays. // given an unsorted array A, make A a heap BUILD-MAX-HEAP (A) 1 A.heap-size = A.length 2 for i = [A.length/2] downto 1 3 MAX-HEAPIFY (A, i) 15
  • 16.
    BUILD-MAX-HEAP() - Example •Example: Building a max-heap from the following unsorted array results in the first heap example. (Work through example: A = {4, 1, 3, 2, 16, 9, 10, 14, 8, 7})  i starts off as 5. (n=10, n/2=5)  MAX-HEAPIFY is applied to subtrees rooted at nodes (in order): 16, 2, 3, 1, 4.  A = {16, 14, 10, 8, 7, 9, 3, 2, 4, 1}
  • 17.
  • 18.
  • 19.
    Heapsort • Given BuildHeap(),an in-place sorting algorithm is easily constructed:  Maximum element is at A[1].  Discard by swapping with element at A[n]. o Decrement heap_size[A]. o A[n] now contains correct value.  Restore heap property at A[1] by calling Heapify().  Repeat, always swapping A[1] for A[heap_size(A)]. 19
  • 20.
    Heapsort HEAPSORT(A) 1 BUILD-MAX-HEAP(A) 2 fori = A.length downto 2 3 exchange A[1] with A[i] 4 A.heap-size = A.heap-size - 1 5 MAX-HEAPIFY(A, 1) 20
  • 21.
    HEAPSORT( ) -Example • The heapsort algorithm starts by using BUILD-MAX-HEAP to build a max-heap on the input array A[1 … n], where n = A.length. • A = {16, 14, 10, 8, 7, 9, 3, 2, 4, 1} 21 The operation of HEAPSORT. (a) The max-heap data structure just after BUILD-MAX-HEAP has built it in line 1.
  • 22.
    HEAPSORT( ) -Example 22 i= 10 i = 9  Given an input array, the heapsort algorithm acts as follows: o Builds a max-heap from the array. o Starting with the root (the maximum element), the algorithm places the maximum element into the correct place in the array by swapping it with the element in the last position in the array. o “Discard” this last node (knowing that it is in its correct place) by decreasing the heap size, and calling MAX-HEAPIFY on the new (possibly incorrectly- placed) root. o Repeat this "discarding" process until only one node (the smallest element) remains, and therefore is in the correct place in the array.
  • 23.
    HEAPSORT( ) -Example 23 A = {7, 4, 3, 1, 2, 8, 9, 10, 14, 16} i = 6 i = 7 i = 8 i = 9 i = 10
  • 24.
    HEAPSORT( ) -Example 24
  • 25.
    HEAPSORT( ) –Example (2) 25
  • 26.
    Outlines: Heap Sort •Input: One-Dimension Array. • Advantages of Insertion Sort and Merge Sort • Heap Sort:  The Heap Property.  Heapify Function “MAX-HEAPIFY (A, i)”  Build Heap Function “BUILD-MAX-HEAP(A)”  Heap Sort Function. • Max-Priority Queues (Basic Operations):  Maximum  Extract-Max  Increase Key  Insert Key 26
  • 27.
    Max-Priority Queues • Adata structure for maintaining a set S of elements, each with an associated value called a key. • Applications: o Scheduling jobs on a shared computer. o Prioritizing events to be processed based on their predicted time of occurrence. o Printer queue. • Heap can be used to implement a max-priority queue. 27
  • 28.
    Max-Priority Queue: BasicOperations • Maximum(S): → return A[1] o returns the element of S with the largest key (value). • Extract-Max(S): o removes and returns the element of S with the largest key. • Increase-Key(S, x, k): o increases the value of element x’s key to the new value k, x.value ≤ k. • Insert(S, x): o inserts the element x into the set S, (i.e., S → S ∪ {x}). 28
  • 29.
    Finding the maximumelement • Getting the maximum element is easy: it’s the root. HEAP-MAXIMUM (A) 1 return A[1] • Time: Θ(1). 29
  • 30.
    HEAP-EXTRACT-MAX (A) • Theprocedure HEAP-EXTRACT-MAX implements the EXTRACT- MAX operation. HEAP-EXTRACT-MAX (A) 1 if A.heap-size < 1 // zero elements 2 error “heap underflow” 3 max = A[1] // max element in first position 4 A[1] = A[A.heap-size] // value of last position assigned to first position 5 A.heap-size = A.heap-size - 1 6 MAX-HEAPIFY(A, 1) 7 return max 30 Note: lines 4-6 are similar to the for loop body of Heapsort procedure.
  • 31.
    HEAP-INCREASE-KEY(A, i, key) //increase a value (key) in the array HEAP-INCREASE-KEY(A, i, key) 1 if key < A[i] 2 error “new key is smaller than current key” 3 A[i] = key 4 while i > 1 and A[Parent (i)] < A[i] 5 exchange A[i] with A[Parent (i)] 6 i = Parent (i) // move index up to parent 31
  • 32.
    Increase-Key(A, 4, 15)Example • A = {16, 14, 10, 8, 7, 9, 3, 2, 4, 1}. 32 i=10i=9
  • 33.
    Increase-Key(A, 4, 15)Example • A = {16, 14, 10, 8, 7, 9, 3, 2, 15, 1}. • The index i increased to 15. 33
  • 34.
    Increase-Key(A, 4, 15)Example • A = {16, 14, 10, 15, 7, 9, 3, 2, 8, 1}. • After one iteration of the while loop of lines 4-6, the node and its parent have exchanged keys (values), and the index i moves up to the parent. 34 i=4
  • 35.
    Increase-Key(A, 4, 15)Example • A = {16, 15, 10, 14, 7, 9, 3, 2, 8, 1}. • After one more iteration of the while loop. • The max-heap property now holds and the procedure terminates. 35 i=2
  • 36.
  • 37.
    MAX-HEAP-INSERT (A) // inserta value at the end of the binary tree then move it in the right position MAX-HEAP-INSERT(A, key) 1 A.heap-size = A.heap-size + 1 2 A[A.heap-size]= -∞ 3 HEAP-INCREASE-KEY(A, A.heap-size, key) 37
  • 38.
    Example: Operation ofHeap Insert • Figure x: The operation of HEAP-INSERT. (a) The heap of Figure (a) before we insert a node with key 15. (b) A new leaf is added to the tree. (c) Values on the path from the new leaf to the root are copied down until a place for the key 15 is found. (d) The key 15 is inserted. 38
  • 39.
    Heap Sort Dr. Ra’FatA. AL-msie’deen Chapter 3 Mutah University Faculty of IT, Department of Software Engineering