Presentation is loading. Please wait.

Presentation is loading. Please wait.

Design and Analysis of Algorithms

Similar presentations


Presentation on theme: "Design and Analysis of Algorithms"— Presentation transcript:

1 Design and Analysis of Algorithms
Dr. Maheswari Karthikeyan 09/03/2013, Lecture 6

2 HeapSort

3 HEAPSORT Heap - The heap data structure is an array object which can be viewed as a nearly complete binary tree. - the two attributes of heap are length[A] and heap-size[A] Heap as an ARRAY - given an index i of a node, Parent(i) return (i/2) Left(i) return 2i Right(i) return 2i + 1 17 PARENT LEFT CHILD RIGHT CHILD 5 13

4 HEAPSORT Max-heap - max-heap property is A[parent(i)] >= A[i] Min-heap - min-heap property is A[parent(i)] <= A[i] max-heap min-heap 17 6 5 13 12 14

5 HEAPSORT Basic Procedures used in Heapsort algorithm
MAX-HEAPIFY – maintains the heap property. BUILD-MAX-HEAP – produces a max-heap from an unordered input array. HEAPSORT – sorts an array in place

6 HEAPSORT MAX-HEAPIFY (A,i) l  left(i) r  right(i) if l <= heap-size[A] and A[l] > A[i] then largest  l else largset  i if r <= heap-size[A] and A[r] > A[largest] then largest  r if largest ≠ i then exchange A[i] <-> A[largest] MAX-HEAPIFY (A,largest)

7 MAX-HEAPIFY MAX-HEAPIFY( A,2) 1 4 3 2 33 1 5 6 7 4 15 16 9 19 10 9 8
14 7 11 MAX-HEAPIFY( A,2)

8 MAX-HEAPIFY MAX-HEAPIFY( A,4) 1 4 3 2 3 19 5 6 7 4 15 16 9 1 10 9 8 14
11 MAX-HEAPIFY( A,4)

9 MAX-HEAPIFY MAX-HEAPIFY( A,8) 1 4 3 2 3 19 5 6 7 4 15 16 9 14 10 9 8 1
11 MAX-HEAPIFY( A,8)

10 MAX-HEAPIFY MAX-HEAPIFY( A,1 ) 1 4 3 2 9 30 5 6 7 4 24 2 8 25 10 9 8
20 12 22 MAX-HEAPIFY( A,1 )

11 MAX-HEAPIFY MAX-HEAPIFY( A,1 ) 1 30 3 2 9 25 5 6 7 4 24 2 8 22 10 9 8
20 12 4 MAX-HEAPIFY( A,1 )

12 Building a HEAP BUILD-MAX-HEAP (A) Heap-size[A]  length[A] for i  length[A]/2 downto 1 do MAX-HEAPIFY (A,i)

13 Building a HEAP 4 1 3 19 13 16 9 14 7 11 1 4 3 2 3 1 5 6 7 4 13 16 9 19 10 9 8 14 7 11

14 Building a HEAP 4 1 3 19 15 16 9 14 7 11 MAX-HEAPIFY( A,5) 1 4 3 2 3 1
13 16 9 19 10 9 8 14 7 11 MAX-HEAPIFY( A,5)

15 Building a HEAP 4 1 3 19 15 16 9 14 7 11 MAX-HEAPIFY( A,4) 1 4 3 2 3 1
13 16 9 19 10 9 8 14 7 11 MAX-HEAPIFY( A,4)

16 Building a HEAP 4 1 3 19 15 16 9 14 7 11 MAX-HEAPIFY( A,3) 1 4 3 2 3 1
13 16 9 19 10 9 8 14 7 11 MAX-HEAPIFY( A,3)

17 Building a HEAP 4 1 3 19 15 16 9 14 7 11 MAX-HEAPIFY( A,2) 1 4 3 2 16
13 3 9 19 10 9 8 14 7 11 MAX-HEAPIFY( A,2)

18 Building a HEAP 4 1 3 19 15 16 9 14 7 11 MAX-HEAPIFY( A,4) 1 4 3 2 16
13 3 9 1 10 9 8 14 7 11 MAX-HEAPIFY( A,4)

19 Building a HEAP 4 1 3 19 15 16 9 14 7 11 MAX-HEAPIFY( A,1) 1 4 3 2 16
13 3 9 14 10 9 8 1 7 11 MAX-HEAPIFY( A,1)

20 Building a HEAP 4 1 3 19 15 16 9 14 7 11 MAX-HEAPIFY( A,2) 1 19 3 2 16
13 3 9 14 10 9 8 1 7 11 MAX-HEAPIFY( A,2)

21 Building a HEAP 4 1 3 19 15 16 9 14 7 11 MAX-HEAPIFY( A,4) 1 19 3 2 16
13 3 9 4 10 9 8 1 7 11 MAX-HEAPIFY( A,4)

22 Building a HEAP 4 1 3 19 15 16 9 14 7 11 1 19 3 2 16 14 5 6 7 4 13 3 9 7 10 9 8 1 4 11 DONE

23 Building a HEAP 5 3 17 10 84 19 6 22 9 BUILD a heap on the above array

24 Building a HEAP 5 3 17 10 84 19 6 22 9 1 84 3 2 19 22 5 6 7 4 3 17 6 10 9 8 5 9

25 HeapSort algorithm HEAPSORT(A) BUILD-MAX-HEAP(A) for i  length[A] downto 2 do exchange A[1] <-> A[i] heap-size [A]  heap-size[A] – 1 MAX-HEAPIFY(A,1)

26 HeapSort algorithm 19 14 16 7 13 3 9 1 4 11 1 19 3 2 16 14 5 6 7 4 13 3 9 7 10 9 8 1 4 11 A

27 HeapSort algorithm 19 14 16 7 13 3 9 1 4 11 1 11 3 2 16 14 5 6 7 4 13 3 9 7 9 8 1 4 A 19

28 HeapSort algorithm 19 14 16 7 13 3 9 1 4 11 1 16 3 2 11 14 5 6 7 4 13 3 9 7 9 8 1 4 A 19

29 HeapSort algorithm 19 14 16 7 13 3 9 1 4 11 1 4 3 2 11 14 5 6 7 4 13 3 9 7 8 1 A 16 19

30 HeapSort algorithm 19 14 16 7 13 3 9 1 4 11 1 14 3 2 11 13 5 6 7 4 4 3 9 7 8 1 A 16 19

31 19 14 16 7 13 3 9 1 4 11 1 1 3 2 11 13 5 6 7 4 4 3 9 7 A 14 16 19

32 HeapSort algorithm 19 14 16 7 13 3 9 1 4 11 1 13 3 2 11 7 5 6 7 4 4 3 9 1 A 14 16 19

33 HeapSort algorithm 19 14 16 7 13 3 9 1 4 11 1 13 3 2 11 7 5 6 7 4 4 3 9 1 A 14 16 19

34 HeapSort algorithm 19 14 16 7 13 3 9 1 4 11 1 9 3 2 11 7 5 6 4 4 3 1 A 13 14 16 19

35 HeapSort algorithm 19 14 16 7 13 3 9 1 4 11 1 11 3 2 9 7 5 6 4 4 3 1 A 13 14 16 19

36 HeapSort algorithm 19 14 16 7 13 3 9 1 4 11 1 3 3 2 9 7 5 4 4 1 A 11 13 14 16 19

37 HeapSort algorithm 19 14 16 7 13 3 9 1 4 11 1 9 3 2 3 7 5 4 4 1 A 11 13 14 16 19

38 HeapSort algorithm 19 14 16 7 13 3 9 1 4 11 1 4 3 2 3 7 4 1 A 09 11 13 14 16 19

39 HeapSort algorithm 19 14 16 7 13 3 9 1 4 11 1 7 3 2 3 4 4 1 A 09 11 13 14 16 19

40 HeapSort algorithm 19 14 16 7 13 3 9 1 4 11 1 1 3 2 3 4 A 07 09 11 13 14 16 19

41 HeapSort algorithm 19 14 16 7 13 3 9 1 4 11 1 4 3 2 3 1 A 07 09 11 13 14 16 19

42 HeapSort algorithm 19 14 16 7 13 3 9 1 4 11 1 3 2 1 A 04 07 09 11 13 14 16 19

43 19 14 16 7 13 3 9 1 4 11 A 01 03 04 07 09 11 13 14 16 19

44 RUNNING TIME OF HEAPSORT
RUNNING TIME OF MAX-HEAPIFY Running time of MAX-HEAPIFY depends on the height h of a heap i.e O(h) Height of a heap = lg n T(n) = O(lg n)

45 RUNNING TIME OF HEAPSORT
HEAPSORT(A) BUILD-MAX-HEAP(A) for i  length[A] downto 2 do exchange A[1] <-> A[i] heap-size [A]  heap-size[A] – 1 MAX-HEAPIFY(A,1) Running time of BUILD-MAX-HEAP is O(n) MAX-HEAPIFY is executed n-1 times with running time O(log n) Total Running time of Heap Sort is O(n) + n-1(O(log n) = O(n log n)

46 RUNNING TIME OF HEAPSORT
Running time of BULID-MAX-HEAP lg n lg n Total cost of BULID-MAX-HEAP is ∑ n/2 h+1 O(h) = O (n ∑ h/2 h) h= h=0 ∑ h/2 h = ½ = 2. h= (1- ½)2 Running time of BUILD-MAX-HEAP can be bounded as lg n ∞ O (n ∑ h/2 h) = O(n ∑ h/2 h) = O (n) . h= h=0


Download ppt "Design and Analysis of Algorithms"

Similar presentations


Ads by Google