0% found this document useful (0 votes)
79 views12 pages

Design & Analysis of Algorithm (CSC-321) : Computer Sciences Department Bahria University (Karachi Campus)

The document discusses the HeapSort algorithm. [1] It explains how to build a max heap from an array by having each parent node be greater than its children. [2] The HeapSort algorithm first builds a max heap from the input array, then repeatedly removes the maximum element and inserts it into the sorted part of the array, while maintaining the heap property. [3] Both building the heap and each removal/insertion step take O(logn) time, so the overall time complexity of HeapSort is O(nlogn).

Uploaded by

Abdullah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views12 pages

Design & Analysis of Algorithm (CSC-321) : Computer Sciences Department Bahria University (Karachi Campus)

The document discusses the HeapSort algorithm. [1] It explains how to build a max heap from an array by having each parent node be greater than its children. [2] The HeapSort algorithm first builds a max heap from the input array, then repeatedly removes the maximum element and inserts it into the sorted part of the array, while maintaining the heap property. [3] Both building the heap and each removal/insertion step take O(logn) time, so the overall time complexity of HeapSort is O(nlogn).

Uploaded by

Abdullah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Design & Analysis of Algorithm (CSC-321)

Lecture 6
Computer Sciences Department
Bahria University (Karachi Campus)
HeapSort
Heap
• Min heap

• Max heap
Max heap
Building a Heap
• Convert an array A[1 … n] into a max-heap (n = length[A])
• The elements in the subarray A[(n/2+1) .. n] are leaves
• Apply MAX-HEAPIFY on elements between 1 and n/2

Alg: BUILD-MAX-HEAP(A) 1

1. n = length[A] 4
2 3
2. for i ← n/2 downto 1 1 3
4 5 6 7

3. do MAX-HEAPIFY(A, i, n) 8
2 9 10
16 9 10
14 8 7

A: 4 1 3 2 16 9 10 14 8 7

5
Running Time of BUILD MAX HEAP
Alg: BUILD-MAX-HEAP(A)
1. n = length[A]
2. for i ← n/2 downto 1
O(n)
3. do MAX-HEAPIFY(A, i, n) O(lgn)

 Running time: O(nlgn)


• This is not an asymptotically tight upper
bound

7
Running Time of BUILD MAX HEAP
• HEAPIFY takes O(h)  the cost of HEAPIFY on a node i is
proportional to the height of the node i in the tree
h
 T (n)   ni hi   2i h  i   O(n)
h

Height i 0 i 0 Level No. of nodes


h0 = 3 (lgn) i=0 20

h1 = 2 i=1 21

h2 = 1 i=2 22

h3 = 0 i = 3 (lgn) 23

hi = h – i height of the heap rooted at level i


ni = 2i number of nodes at level i
8
Running Time of BUILD MAX HEAP
h
T (n)   ni hi Cost of HEAPIFY at level i  number of nodes at that level
i 0
h
  2i h  i  Replace the values of ni and hi computed before
i 0
h
hi h
 h i
2 Multiply by 2h both at the nominator and denominator and
i 0 2 write 2i as 1i
2
h
k
2  k
h
Change variables: k = h - i
k 0 2

k
 n k
The sum above is smaller than the sum of all elements to 
k 0 2 and h = lgn

 O(n) The sum above is smaller than 2

Running time of BUILD-MAX-HEAP: T(n) = O(n)


9
Heapsort
• Goal:
– Sort an array using heap representations

• Idea:
– Build a max-heap from the array
– Swap the root (the maximum element) with the last
element in the array
– “Discard” this last node by decreasing the heap size
– Call MAX-HEAPIFY on the new root
– Repeat this process until only one node remains
10
Example: A=[7, 4, 3, 1, 2]

MAX-HEAPIFY(A, 1, 4) MAX-HEAPIFY(A, 1, 3) MAX-HEAPIFY(A, 1, 2)

MAX-HEAPIFY(A, 1, 1)

11
Alg: HEAPSORT(A)

1. BUILD-MAX-HEAP(A) O(n)
2. for i ← length[A] downto 2
3. do exchange A[1] ↔ A[i] n-1 times

4. MAX-HEAPIFY(A, 1, i - 1) O(lgn)

• Running time: O(nlgn) --- Can


be shown to be Θ(nlgn)
12

You might also like