05 - QuickSort and Others Data Structures and Algorithms...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
QuickSort and Others Data Structures and Algorithms Andrei Bulatov
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Algorithms – Quicksort 5-2 Heap Property A heap is a nearly complete binary tree, satisfying an extra condition Let Parent(i) denote the parent of the vertex i Max-Heap Property : Key(Parent(i)) Key(i) for all i in eap Property Min-Heap Property : Key(Parent(i)) Key(i) for all i 1 4 2 3 9 7 8 14 10 16
Background image of page 2
Algorithms – Quicksort 5-3 Insertion Insert(H,key) set n:=length(n), set H[n+1]:=key HeapifyUp(H,n+1) eapifyUp(H,i) H is almost heap with H[i] too big if decreasing H[i] by a certain amount turns H into a heap HeapifyUp(H,i) if i>1 then set j:=parent(i)= i/2 if Key[H[i]]>Key[H[j]] then swap array entries H[i] and H[j] HeapifyUp(H,j) endif endif
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Algorithms – Quicksort 5-4 Deletion Delete(H,i) set n:=length(n), set H[i]:=H[n] if Key[H[i]]>Key[H[parent(i)]] then HeapifyUp(H,i) ndif endif if Key[H[i]]<Key[H[leftChild(i)]] or Key[H[i]]<Key[H[rightChild(i)]] then HeapifyDown(H,i) endif H is almost heap with H[i] too small if increasing H[i] by a certain amount turns H into a heap
Background image of page 4
Algorithms – Quicksort 5-5 Deletion (cntd) HeapifyDown(H,i) set n:=length(H) if 2i>n then Terminate with H unchanged else if 2i<n then do set left:=2i, right:=2i+1 et j be the index that minimizes Key[H[left]] let j be the index that minimizes Key[H[left]] and Key[H[right]] else if 2i=n then set j:=2i endif if Key[H[j]]>Key[H[i]] then swap array entries H[i] and H[j] HeapifyDown(H,j) endif
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
5-6 HeapifyDown: Soundness Theorem The procedure HeapifyDown(H,i) fixes the heap property in O(log i) time, assuming that the array H is almost a heap with the key of H[i] too small. The running time of
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/19/2009 for the course CS CMPT 307 taught by Professor A.bulatov during the Fall '09 term at Simon Fraser.

Page1 / 24

05 - QuickSort and Others Data Structures and Algorithms...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online