quicksort

quicksort - Quicksort Algorithm Algorithm Quicksort(A p r 1...

• Notes
• 5

This preview shows pages 1–3. Sign up to view the full content.

Algorithms and Data Structures: Average-Case Analysis of Quicksort 15th October, 2010 ADS: lect 6 – slide 1 – 15th October, 2010 Quicksort Divide-and-Conquer algorithm for sorting an array. It works as follows: 1. If the input array has less than two elements, there is nothing to do. Otherwise, do the following partitioning subroutine: Pick a particular key called the pivot and divide the array into two subarrays as follows: pivot piv. pivot 2. Sort the two subarrays recursively. ADS: lect 6 – slide 2 – 15th October, 2010 Quicksort Algorithm Algorithm Quicksort ( A , p , r ) 1. if p < r then 2. q Partition ( A , p , r ) 3. Quicksort ( A , p , q - 1 ) 4. Quicksort ( A , q + 1 , r ) ADS: lect 6 – slide 3 – 15th October, 2010 Partitioning Algorithm Partition ( A , p , r ) 1. pivot A [ r ] 2. i p - 1 3. j r 4. while true do 5. do i i + 1 while A [ i ] < pivot end 6. do j j - 1 while A [ j ] > pivot and j > p end 7. if i j then 8. break 9. exchange A [ i ] , A [ j ] 10. end 11. exchange A [ i ] , A [ r ] 12. return i ADS: lect 6 – slide 4 – 15th October, 2010

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

Remarks on Partitioning The partitioning subroutine on the previous slide looks a bit clumsy, but it is considered to be fastest in practice. It is taken from [Sedgewick]. [CLRS] describes an alternative partitioning method, which is more elegant and easier to analyse, but slightly less efficient. The first edition of [CLRS] describes yet another method, which is also less efficient. You can find this method in the CS2 lecture notes. ADS: lect 6 – slide 5 – 15th October, 2010 Analysis of Quicksort The size of an instance ( A , p , r ) is n = r - p + 1. Basic operations for sorting are comparisons of keys . We let C ( n ) be the worst-case number of key-comparisons done by Quicksort ( A , p , r ) . We shall try to determine C ( n ) as precisely as possible.
This is the end of the preview. Sign up to access the rest of the document.
• Summer '07
• staff
• Analysis of algorithms, Best, worst and average case

{[ snackBarMessage ]}

What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern