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

Info icon This 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
Image of page 1

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

View Full Document Right 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 Min-Heap Property : Key(Parent(i)) Key(i) for all i 1 4 2 3 9 7 8 14 10 16
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) HeapifyUp(H,i) H is almost heap with H[i] too big if decreasing H[i] by a certain amount turns H into a heap 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
Image of page 3

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

View Full Document Right 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) 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
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 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
Image of page 5

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

View Full Document Right Arrow Icon
Algorithms – Quicksort 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.
Image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern