solution2 - CMPT 307 Data Structures and Algorithms Outline...

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

View Full Document Right Arrow Icon
CMPT 307 — Data Structures and Algorithms Outline Solutions to Exercises on Sorting 1. Show that the worst-case running time of Heapify-Up on a heap of size n is Ω(log n ) . If the new value added to the very end of the heap is smaller than any other element of the heap, then Heapify- Up will move it to the root of the heap. Since the length of root-to leaf paths in a heap is at least log n - 1 , Heapify-Up perfoms log n - 1 swaps of elements that takes Ω(log n ) time. 2. (a) What is the running time of Quicksort whe all elements of array A have the same value? (b) Show that the running time of Quicksort is Θ( n 2 ) when the array A contains distinct elements and is ordered in decreasing order. (a) The Partition procedure splits the array into two parts, one of which, the upper oner, is always empty. More precisely, given a subarray A [ p . . . r ] of equal elements, produces empty partition in A [ q + 1 . . .r - 1] , puts the pivot (originally in A [ r ] ) into A [ r ] , and produces a partition A [ p . . .r - 1] with only one fewer element than A [ p . . . r ] . Therefore on each recursive call the size of the array is decreased only by 1. So the recurrence relation for Quicksort becomes T ( n ) = T ( n - 1) + Θ( n ) , which has the solution T ( n ) = Θ( n 2 ) . (b) The Partition procedure splits the array into two parts, one of which, the lower oner, is always empty. More precisely, given a subarray A [ p . . . r ] of distinct elements in decreasing order, produces empty partition in A [ p . . .q - 1] , puts the pivot (originally in A [ r
Background image of page 1

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

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 3

solution2 - CMPT 307 Data Structures and Algorithms Outline...

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

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