quicksort

# quicksort - Quicksort Algorithm Algorithm Quicksort(A, p ,...

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.

View Full Document
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 eﬃcient. ± The ﬁrst edition of [CLRS] describes yet another method, which is also less eﬃcient. You can ﬁnd 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. ± It is easy to see that the worst-case running time
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 12/02/2010 for the course CMSC 351 taught by Professor Staff during the Spring '07 term at Maryland.

### Page1 / 5

quicksort - Quicksort Algorithm Algorithm Quicksort(A, p ,...

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

View Full Document
Ask a homework question - tutors are online