CPSC 311 Section 502 Analysis of Algorithm Fall 2002 Department of Computer Science Texas A&M University.

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

BY Lecturer: Aisha Dawood. Heapsort  O(n log n) worst case like merge sort.  Sorts in place like insertion sort.  Combines the best of both algorithms.
Analysis of Algorithms
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2009 Heap Lecture Chapter 6 Use NOTES feature to see explanation.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
CS 253: Algorithms Chapter 6 Heapsort Appendix B.5 Credit: Dr. George Bebis.
Analysis of Algorithms CS 477/677
More sorting algorithms: Heap sort & Radix sort. Heap Data Structure and Heap Sort (Chapter 7.6)
Comp 122, Spring 2004 Heapsort. heapsort - 2 Lin / Devi Comp 122 Heapsort  Combines the better attributes of merge sort and insertion sort. »Like merge.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 7 Heapsort and priority queues Motivation Heaps Building and maintaining heaps.
Analysis of Algorithms CS 477/677 Midterm Exam Review Instructor: George Bebis.
Heapsort Chapter 6. Heaps A data structure with  Nearly complete binary tree  Heap property: A[parent(i)] ≥ A[i] eg. Parent(i) { return } Left(i) {
1 Priority Queues A priority queue is an ADT where: –Each element has an associated priority –Efficient extraction of the highest-priority element is supported.
3-Sorting-Intro-Heapsort1 Sorting Dan Barrish-Flood.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2007 Heap Lecture Chapter 6 Use NOTES feature to see explanation.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
David Luebke 1 7/2/2015 Merge Sort Solving Recurrences The Master Theorem.
PQ, binary heaps G.Kamberova, Algorithms Priority Queue ADT Binary Heaps Gerda Kamberova Department of Computer Science Hofstra University.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Heap Lecture 2 Chapter 7 Wed. 10/10/01 Use NOTES feature to.
Heapsort CIS 606 Spring Overview Heapsort – O(n lg n) worst case—like merge sort. – Sorts in place—like insertion sort. – Combines the best of both.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 15.
Sorting Algorithms (Part II) Slightly modified definition of the sorting problem: input: A collection of n data items where data item a i has a key, k.
2IL50 Data Structures Spring 2015 Lecture 3: Heaps.
Analyzing Recursive Algorithms A recursive algorithm can often be described by a recurrence equation that describes the overall runtime on a problem of.
David Luebke 1 10/3/2015 CS 332: Algorithms Solving Recurrences Continued The Master Theorem Introduction to heapsort.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
Heaps, Heapsort, Priority Queues. Sorting So Far Heap: Data structure and associated algorithms, Not garbage collection context.
Binary Heap.
2IL50 Data Structures Fall 2015 Lecture 3: Heaps.
Sorting. Pseudocode of Insertion Sort Insertion Sort To sort array A[0..n-1], sort A[0..n-2] recursively and then insert A[n-1] in its proper place among.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
Data Structure & Algorithm Lecture 5 Heap Sort & Binary Tree JJCAO.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
1 Analysis of Algorithms Chapter - 03 Sorting Algorithms.
1 Algorithms CSCI 235, Fall 2015 Lecture 14 Analysis of Heap Sort.
David Luebke 1 12/23/2015 Heaps & Priority Queues.
Computer Algorithms Lecture 9 Heapsort Ch. 6, App. B.5 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
Heapsort. What is a “heap”? Definitions of heap: 1.A large area of memory from which the programmer can allocate blocks as needed, and deallocate them.
Midterm Review 1. Midterm Exam Thursday, October 15 in classroom 75 minutes Exam structure: –TRUE/FALSE questions –short questions on the topics discussed.
Lecture 15 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
CSC 413/513: Intro to Algorithms Solving Recurrences Continued The Master Theorem Introduction to heapsort.
Chapter 6: Heapsort Combines the good qualities of insertion sort (sort in place) and merge sort (speed) Based on a data structure called a “binary heap”
1 Heap Sort. A Heap is a Binary Tree Height of tree = longest path from root to leaf =  (lgn) A heap is a binary tree satisfying the heap condition:
David Luebke 1 2/5/2016 CS 332: Algorithms Introduction to heapsort.
Analysis of Algorithms CS 477/677 Lecture 8 Instructor: Monica Nicolescu.
COSC 3101A - Design and Analysis of Algorithms 3 Recurrences Master’s Method Heapsort and Priority Queue Many of the slides are taken from Monica Nicolescu’s.
CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is.
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
Sept Heapsort What is a heap? Max-heap? Min-heap? Maintenance of Max-heaps -MaxHeapify -BuildMaxHeap Heapsort -Heapsort -Analysis Priority queues.
Heapsort Lecture 4 Asst. Prof. Dr. İlker Kocabaş.
6.Heapsort. Computer Theory Lab. Chapter 6P.2 Why sorting 1. Sometimes the need to sort information is inherent in a application. 2. Algorithms often.
1 Algorithms CSCI 235, Fall 2015 Lecture 13 Heap Sort.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
"Teachers open the door, but you must enter by yourself. "
Heaps, Heapsort, and Priority Queues
Heaps, Heap Sort and Priority Queues
Heapsort.
Heap Sort Example Qamar Abbas.
Introduction to Algorithms
CS 583 Analysis of Algorithms
Heaps, Heapsort, and Priority Queues
CS200: Algorithm Analysis
Ch 6: Heapsort Ming-Te Chi
"Teachers open the door, but you must enter by yourself. "
Topic 5: Heap data structure heap sort Priority queue
HEAPS.
Heapsort Sorting in place
Solving Recurrences Continued The Master Theorem
Algorithms CSCI 235, Spring 2019 Lecture 14 Heap Sort Read: Ch. 6
Presentation transcript:

CPSC 311 Section 502 Analysis of Algorithm Fall 2002 Department of Computer Science Texas A&M University

CPSC 311 O. Burchan Bayazit – Fall 02 Announcements There will be a quiz on next Tuesday (Sep 17) A new homework will be put to the web site today ( due date next Thursday (Sep 19)) Grades for the previous quiz is available

CPSC 311 O. Burchan Bayazit – Fall 02 Solving Recurrences: Master Method The master method provides a 'cookbook' method for solving recurrences of a certain form. Master Theorem: Let a  1 and b > 1 be constants, let f(n) be a function, and let T(n) be defined on nonnegative integers as: T(n) = aT(n/b) + f(n) Then, T(n) can be bounded asymptotically as follows: 1.T(n) =  (n log b a )if f(n) = O(n log b a-  ) for some constant  > 0 2.T(n) =  (n log b a logn) if f(n) =  (n log b a ) 3.T(n) =  ( f(n))if f(n) =  (n log b a+  ) for some constant  > 0 and if af(n/b)  cf(n) for some constant c < 1 and all sufficiently large n.

CPSC 311 O. Burchan Bayazit – Fall 02 Case 3 T(n) =  ( f(n)), if f(n) =  (n log b a+  ) for some constant  > 0 and if af(n/b)  cf(n) for some constant c < 1 and all sufficiently large n. T(n) = aT(n/b) + f(n) T(n) =3T(n/4)+nlgn a=3,b=4,f(n)=nlgn { PART I: Is f(n) =  (n log b a+  ) for some  >0? n log b a+  = n log 4 3+  =n  f(n)=nlgn can we find  > 0 such that nlgn=  (n  ) If we choose  = the lower bound becomes  (n ) And nlgn has a lower bound n i.e., f(nlgn)=  (n ) So the first part holds.

CPSC 311 O. Burchan Bayazit – Fall 02 Case 3 T(n) =  ( f(n)), if f(n) =  (n log b a+  ) for some constant  > 0 and if af(n/b)  cf(n) for some constant c < 1 and all sufficiently large n. T(n) = aT(n/b) + f(n) T(n) =3T(n/4)+nlgn a=3,b=4,f(n)=nlgn { Part II: is there a constant <1 that satisfies af(n/b)  cf(n) ? af(n/b) = 3f(n/4) So we should find a c such 3f(n/4) 1 3f(n/4)= 3(n/4)lg(n/4) and f(n)=nlgn (3/4)n lg(n)-(3/4)lg4 <=c.nlgn If we select c=3/4 this inequality holds so T(n)=  (nlgn)

CPSC 311 O. Burchan Bayazit – Fall 02 Sorting Algorithms Ch Slightly modified definition of the sorting problem: input: A collection of n data items where each data item has a key drawn from a linearly ordered set (e.g., ints, chars) output: A permutation (reordering) of the input sequence such that a' 1  a' 2 ...  a' n In practice, one usually sorts 'records' according to their key (the non-key data is called satellite data.) If the records are large, we may sort an array of pointers.

CPSC 311 O. Burchan Bayazit – Fall 02 Sorting Algorithms A sorting algorithm is in place if only a constant number of elements of the input array are ever stored outside the array. A sorting algorithm is comparison based if the only operation we can perform on keys is to compare two keys.

CPSC 311 O. Burchan Bayazit – Fall 02 Merge Sort A Divide-and-Conquer algorithm Divide: the n-element array to be sorted into two n/2-element subarrays Conquer: Sort the subarrays recursively using merge sort Combine: Merge the two sorted subarrays to find sorted array

CPSC 311 O. Burchan Bayazit – Fall 02 Merge Sort Unsorted original array

CPSC 311 O. Burchan Bayazit – Fall 02 Merge Sort Divide the problem to two subproblems Call merge sort on subarrays

CPSC 311 O. Burchan Bayazit – Fall 02 Merge Sort Further divide subarrays

CPSC 311 O. Burchan Bayazit – Fall 02 Merge Sort Until the smallest array is reached

CPSC 311 O. Burchan Bayazit – Fall 02 Merge Sort Merge results of two subarrays

CPSC 311 O. Burchan Bayazit – Fall 02 Merge Sort Further merge

CPSC 311 O. Burchan Bayazit – Fall 02 Merge Sort Further merge

CPSC 311 O. Burchan Bayazit – Fall 02 Merge Sort Further merge

CPSC 311 O. Burchan Bayazit – Fall 02 Merge Sort Final merge

CPSC 311 O. Burchan Bayazit – Fall 02 Merge Sort Result

CPSC 311 O. Burchan Bayazit – Fall 02 Merge Sort - Algorithm Merge-Sort(A,start,end) 1. if start=end then { 2. return 3. } 4. half =  (start+end)/2  5. Merge-Sort (A,start,half) 6. Merge-Sort (A,half+1,end) 7. Merge (A,start,half,end)

CPSC 311 O. Burchan Bayazit – Fall 02 Merging

CPSC 311 O. Burchan Bayazit – Fall 02 Merging

CPSC 311 O. Burchan Bayazit – Fall 02 Merging

CPSC 311 O. Burchan Bayazit – Fall 02 Merging

CPSC 311 O. Burchan Bayazit – Fall 02 Merging

CPSC 311 O. Burchan Bayazit – Fall 02 Merging

CPSC 311 O. Burchan Bayazit – Fall 02 Merging

CPSC 311 O. Burchan Bayazit – Fall 02 Merging

CPSC 311 O. Burchan Bayazit – Fall 02 Merging

CPSC 311 O. Burchan Bayazit – Fall 02 Merge Algorithm Merge(A,start,half,end) 1. n 1 = half-start+1; n 2  end-half; 2. for i= 1 to n 1 { 3. Left[i]= A[start+i-1] // copy left subarray 4. } 5. for i= 1 to n 2 { 6. R[i]  A[half+i] // copy right subarray 7. } 8. Left[n 1 +1] = Right[n 2 +1] =  9. i= j= for k = start to end { 11. if Left[i]  Right[j] then { 12. A[k]=Left[i] 13. i= i+1 } 14. else { A[k]= Right[j] 15. j= j+1 } 16. } Running Time =  (n)

CPSC 311 O. Burchan Bayazit – Fall 02 Merge Sort - Analysis Merge-Sort(A,start,end) 1. if start=end then { 2. return 3. } 4. half =  (start+end)/2  5. Merge-Sort (A,start,half) 6. Merge-Sort (A,half+1,end) 7. Merge (A,start,half,end)  (1) T (n/2)  (n) T (n)=2T(n/2)+  (n)  (nlgn) using master method

CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort heap concept complete binary tree, except may be missing some rightmost leaves on the bottom level. each node contains a key values in the nodes satisfy the heap property.

CPSC 311 O. Burchan Bayazit – Fall 02 Heap vs. Array binary tree: an array implementation root is A[1] for element A[i] - left child is in position A[2i] - right child is in position A[2i + 1] - parent is in A[  i/2  ] Left child=2i i=2 Right child=2i+1 Parent =i/2

CPSC 311 O. Burchan Bayazit – Fall 02 Max-Heap Max-heap property: A[Parent(i)]  A[i] In the array representation of a max-heap, the root of the tree is in A[1], and given the index i of a node, Parent(i) LeftChild(i)RightChild(i) return (  i/2  ) return (2i) return (2i + 1) A index keys n = 11 height = 3 (# edges on longest leaf to root path)

CPSC 311 O. Burchan Bayazit – Fall 02 Min-Heap Min-heap property: A[Parent(i)]  A[i] Min-heaps are commonly used for priority queues in event-driven simulators. Parent(i) LeftChild(i)RightChild(i) return (  i/2  ) return (2i) return (2i + 1) A index keys n = 11 height = 3 (# edges on longest leaf to root path)

CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works Assume we have a max-heap

CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works The largest number is the root (max-heap property)

CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works Take the root away

CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works Put the last one to the root

CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works Fix the max-heap

CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works Fix the max-heap

CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works Take the root away

CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works Put the last one to the root

CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works Fix the max-heap

CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works Take the root away

CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works Put the last one to the root

CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works Fix the max-heap

CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works Take the root away

CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works Put the last one to the top

CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works Take the top away

CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works Put the last one

CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort Basic Functions HEAPIFY: maintain (fix) max-heap property BUILD-HEAP: Create a max-heap from unsorted array (requires HEAPIFY) HEAPSORT: sorts an array (requires BUILD-HEAP and HEAPIFY) How it works? Build a max-heap (the root is the largest number) Take the largest number away, put the last number in the heap to the root position and if max-heap property is not satisfied, fix it. Repeat until all the elements are processed.

CPSC 311 O. Burchan Bayazit – Fall 02 Heapify (fix max-heap) Assume one node i does not satisfy MAX-HEAP property but its children are. Heapify function corrects this by recursively fixing inconsistencies Not satisfy max-heap

CPSC 311 O. Burchan Bayazit – Fall 02 Assume one node i does not satisfy MAX-HEAP property but its children are. Heapify function corrects this by recursively fixing inconsistencies Switch with the largest child Heapify (fix max-heap)

CPSC 311 O. Burchan Bayazit – Fall 02 Assume one node i does not satisfy MAX-HEAP property but its children are. Heapify function corrects this by recursively fixing inconsistencies Not satisfy max-heap Heapify (fix max-heap)

CPSC 311 O. Burchan Bayazit – Fall 02 Assume one node i does not satisfy MAX-HEAP property but its children are. Heapify function corrects this by recursively fixing inconsistencies Switch with the largest child Heapify (fix max-heap)

CPSC 311 O. Burchan Bayazit – Fall 02 Heapify (fix max-heap) Find the largest child Switch the node i with its largest child Recursively HEAPIFY for the subtree where node i placed

CPSC 311 O. Burchan Bayazit – Fall 02 Heapify(A,i,n) /* A: Array, I: node to be fixed, n:heap-size */ 1. left  2i; right  2i + 1 /*indices of left & right children of A[i] */ 2. largest  i; 3. if left  n and A[left] > A[i] then { 4.largest  left 5.} 5. if right  n and A[right] > A[largest] then { 6.largest  right 7.} 7. if largest  i then 8. swap(A[i], A[largest]) 9. Heapify(A, largest,n) T (n)=T(2n/3)+  (1) O (lgn) using master method Heapify (fix max-heap)

CPSC 311 O. Burchan Bayazit – Fall 02 Build-Heap Given unsorted array Starting from largest non-leaf node down to the first node, call Heapify for each node

CPSC 311 O. Burchan Bayazit – Fall 02 Build-Heap Build-Max-Heap(A,n) 1. for i   n/2  downto 1 { 2.Heapify(A, i,n) 3.}

CPSC 311 O. Burchan Bayazit – Fall 02 Build-Heap Build-Max-Heap(A,n) 1. for i   n/2  downto 1 { 2.Heapify(A, i,n) 3.}

CPSC 311 O. Burchan Bayazit – Fall 02 Build-Heap Build-Max-Heap(A,n) 1. for i   n/2  downto 1 { 2.Heapify(A, i,n) 3.}

CPSC 311 O. Burchan Bayazit – Fall 02 Build-Heap Build-Max-Heap(A,n) 1. for i   n/2  downto 1 { 2.Heapify(A, i,n) 3.}

CPSC 311 O. Burchan Bayazit – Fall 02 Build-Heap Build-Max-Heap(A,n) 1. for i   n/2  downto 1 { 2.Heapify(A, i,n) 3.}

CPSC 311 O. Burchan Bayazit – Fall 02 Build-Heap Build-Max-Heap(A,n) 1. for i   n/2  downto 1 { 2.Heapify(A, i,n) 3.}

CPSC 311 O. Burchan Bayazit – Fall 02 Build-Heap Build-Max-Heap(A,n) 1. for i   n/2  downto 1 { 2.Heapify(A, i,n) 3.}

CPSC 311 O. Burchan Bayazit – Fall 02 Build-Heap Build-Max-Heap(A,n) 1. for i   n/2  downto 1 { 2.Heapify(A, i,n) 3.}

CPSC 311 O. Burchan Bayazit – Fall 02 Build-Heap Build-Max-Heap(A,n) 1. for i   n/2  downto 1 { 2.Heapify(A, i,n) 3.}

CPSC 311 O. Burchan Bayazit – Fall 02 Build-Heap Build-Max-Heap(A,n) 1. for i   n/2  downto 1 { 2.Heapify(A, i,n) 3.}

CPSC 311 O. Burchan Bayazit – Fall 02 Build-Heap Build-Max-Heap(A,n) 1. for i   n/2  downto 1 { 2.Heapify(A, i,n) 3.}

CPSC 311 O. Burchan Bayazit – Fall 02 Build-Heap Build-Max-Heap(A,n) 1. for i   n/2  downto 1 { 2.Heapify(A, i,n) 3.}

CPSC 311 O. Burchan Bayazit – Fall 02 Build-Heap Build-Max-Heap(A,n) 1. for i   n/2  downto 1 { 2.Heapify(A, i,n) 3.}

CPSC 311 O. Burchan Bayazit – Fall 02 Build-Heap Build-Max-Heap(A,n) 1. for i   n/2  downto 1 { 2.Heapify(A, i,n) 3.}

CPSC 311 O. Burchan Bayazit – Fall 02 Build-Heap Build-Max-Heap(A,n) 1. for i   n/2  downto 1 { 2.Heapify(A, i,n) 3.} O(lgn) repeated (n/2 times) T(n)=O(nlgn) Can we do better? Yes T(n)=O(n)

CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort Algorithm HeapSort(A,n) 1. Build-Heap(A,n) /* put all elements in heap */ 2. for i = n downto 2 { 3. swap A[1]  A[i] /* puts max in ith array position */ 4.Heapify(A,1,i-1) /* restore heap property */ 5.} Input: An n-element array A (unsorted). Output: An n-element array A in sorted order, smallest to largest. Running time of HeapSort 1 call to Build-Heap()  O(n) time n-1 calls to Heapify() each takes O(lgn) time  O(nlgn) time

CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works Assume we have a max-heap

CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works The largest number is the root (max-heap property)

CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works Take the root away

CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works Put the last one to the root

CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works Fix the max-heap

CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works Fix the max-heap

CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works Take the root away

CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works Put the last one to the root

CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works Fix the max-heap

CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works Take the root away

CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works Put the last one to the root

CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works Fix the max-heap

CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works Take the root away

CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works Put the last one to the top

CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works Take the top away

CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works Put the last one

CPSC 311 O. Burchan Bayazit – Fall 02 Priority Queues: Application for Heaps An application of max-priority queues is to schedule jobs on a shared processor. Need to be able to get the highest priority job Heap-Maximum(A,n) remove job from the queue Heap-Extract-Max(A,n) insert new jobs into queue Max-Heap-Insert(A, key,n) increase priority of jobs Heap-Increase-Key(A,i,key,n)

CPSC 311 O. Burchan Bayazit – Fall 02 Heap-Extract-Max Heap-Extract-Max(A,n) 1. max=A[1] 2. A[1]=A[n] 3. n=n-1 /* puts max in ith array position */ 4. Heapify(A,1,n) /* restore heap property */ 5. return max T(n)=O(lgn)

CPSC 311 O. Burchan Bayazit – Fall 02 Heap-Increase-Key Heap-Increase-Key(A,i,key,n) 1. A[i]=key 2. while I>1 and A[Parent[i]]<A[i] { 3. swap A[Parent[i]]  A[i] 4. i=Parent[i] T(n)=O(lgn)

CPSC 311 O. Burchan Bayazit – Fall 02 Max-Heap-Insert Max-Heap-Insert(A,i,key,n) 1. n=n+1 2. A[i]=-  3. Heap-Increase-Key(A,n,key,n) T(n)=O(lgn)