1 and the other has size 0 the running time is

Info icon This preview shows pages 12–16. Sign up to view the full content.

1 and the other has size 0 The running time is proportional to the sum n + ( n - 1) + + 2 + 1 Thus, the worst-case running time of quick-sort is O ( n 2 ) depth time 0 n 1 n - 1 n - 1 1
Image of page 12

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

© 2004 Goodrich, Tamassia Quick-Sort 13 Expected Running Time Consider a recursive call of quick-sort on a sequence of size s Good call : the sizes of L and G are each less than 3 s / 4 Bad call : one of L and G has size greater than 3 s / 4 A call is good with probability 1 / 2 1/2 of the possible pivots cause good calls: 7 9 7 1 1 7 2 9 4 3 7 6 1 9 2 4 3 1 7 2 9 4 3 7 6 1 7 2 9 4 3 7 6 1 Good call Bad call 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Good pivots Bad pivots Bad pivots
Image of page 13
© 2004 Goodrich, Tamassia Quick-Sort 14 Expected Running Time, Part 2 Probabilistic Fact: The expected number of coin tosses required in order to get k heads is 2 k For a node of depth i , we expect i / 2 ancestors are good calls The size of the input sequence for the current call is at most ( 3 / 4 ) i / 2 n s ( r ) s ( a ) s ( b ) s ( c ) s ( d ) s ( f ) s ( e ) time per level expected height O (log n ) O ( n ) O ( n ) O ( n ) total expected time: O ( n log n ) Therefore, we have For a node of depth 2log 4 / 3 n , the expected input size is one The expected height of the quick-sort tree is O (log n ) The amount or work done at the nodes of the same depth is O ( n ) Thus, the expected running time of quick-sort is O ( n log n )
Image of page 14

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

© 2004 Goodrich, Tamassia Quick-Sort 15 In-Place Quick-Sort Quick-sort can be implemented to run in-place In the partition step, we use replace operations to rearrange the elements of the input sequence such that the elements less than the pivot have rank less than h the elements equal to the pivot have rank between h and k the elements greater than the
Image of page 15
Image of page 16
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