{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This 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 ] ) into A [ p ] , and produces a partition A [ p + 1 ...r ] with only one
Image of page 1

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

View Full Document Right Arrow Icon
Image of page 2
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