{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Eric-Wed - CSE 3101 Introduction to the Design and Analysis...

Info icon This preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
1 02/14/12 CSE 3101 1 Suprakash Datta datta[at]cse.yorku.ca CSE 3101: Introduction to the Design and  Analysis of Algorithms
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
2 Quick Sort Characteristics sorts ”almost” in place, i.e., does not require an additional array, like insertion sort Divide-and-conquer, like merge sort very practical, average sort performance O(n log n) (with small constant factors), but worst case O(n 2 ) [ CAVEAT: this is true for the CLRS version ]
Image of page 2
3 Quick Sort – the main idea To understand quick-sort, let’s look at a high- level description of the algorithm A divide-and-conquer algorithm Divide: partition array into 2 subarrays such that elements in the lower part <= elements in the higher part Conquer: recursively sort the 2 subarrays Combine: trivial since sorting is done in place
Image of page 3

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

View Full Document Right Arrow Icon
4 Partitioning Linear time partitioning procedure Partition (A,p,r) 01 x A[r] 02 i p-1 03 j r+1 04 while TRUE 05 repeat j j-1 06 until A[j] x 07 repeat i i+1 08 until A[i] x 09 if i<j 10 then exchange A[i] A[j] 11 else return j 17 12 6 19 23 8 5 10 i j 10 12 6 19 23 8 5 17 j i 10 5 6 19 23 8 12 17 j i i j 10 5 6 8 23 19 12 17 X=10
Image of page 4
5 Quick Sort Algorithm Initial call Quicksort(A, 1, length[A]) Quicksort(A,p,r) 01 if p<r 02 then q Partition(A,p,r) 03 Quicksort(A,p,q) 04 Quicksort(A,q+1,r)
Image of page 5

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

View Full Document Right Arrow Icon
6 Analysis of Quicksort Assume that all input elements are distinct The running time depends on the distribution of splits
Image of page 6
7 Best Case If we are lucky, Partition splits the array evenly ( ) 2 ( / 2) ( ) T n T n n = + Θ
Image of page 7

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

View Full Document Right Arrow Icon
8 Using the median as a pivot
Image of page 8
Image of page 9
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