This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: h =  log (n+1)  h = O(log n) Operation with heaps Heaps insertion Original Insert element(2,e) 1 Insert in next available position 2. Fix heap bottom up (upheap) swap parent –child keys are out of order from new node to root. #swaps <= n1 #swaps = O(log n) insert elemento (k,e) takes O(log n) remove min original e = remove min element() 1. removing min element leaves a hole replace with last key of heap 2. Fix the heap topdown (down heap) Swap keys of parent and child if they are out of order starting from root to the bottom of replaced node Return s 2 Remove Min Element Again Return 3 #of comparison and swaps is O(log n) remove min Element() is O(log n) We may use a heap to do sorting of sequences Phase 1 for each element in Sremove element is SInsert element in H(heap) Insert element in heap O(log n) n times = O(n log n) Phase 2 For each element in Heapremove min Element from HInsert H into s Remove Min Element in heap O(log n) N times = O(n log n) Phase 1 Phase 2 O(n log n) + O(n log n) = O(n log n) Sorting using a heap takes O(n log n) This sorting algorithm is called “heap sort” Implementation of a heap using an array Parents is in I Left child is in 2i Right child is in 2i+1...
View
Full Document
 Fall '08
 Staff
 Data Structures, Binary heap, min element

Click to edit the document details