Quicksort - Analysis of Algorithms - Lecture Slides, Slides of Design and Analysis of Algorithms

Main points of this lecture are: Quicksort, Worst-Case Running Time, Expected Running Time, Process of Divide-And-Conquer, Second Subarray, Recursive Calls, Partition Subarray, Correctness of Partition, Time for Partitioning, Performance of Quicksort

Typology: Slides

2012/2013

Uploaded on 04/23/2013

asmita
asmita 🇮🇳

4.6

(34)

178 documents

1 / 19

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Quicksort
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13

Partial preview of the text

Download Quicksort - Analysis of Algorithms - Lecture Slides and more Slides Design and Analysis of Algorithms in PDF only on Docsity!

Quicksort

Quicksort

• Worst-case running time: Θ( n^2 ).

• Expected running time: Θ( n lg n ).

• Constants hidden in Θ( n lg n ) are small.

• Sorts in place.

Quicksort

  • Perform the divide step by a procedure

PARTITION, which returns the index q that marks

the position separating the subarrays.

QUICKSORT( A , p , r )

if p < r

q = PARTITION( A , p , r )

QUICKSORT( A , p , q - 1)

QUICKSORT( A , q + 1, r )

  • Initial call is QUICKSORT( A , 1, n )

Partitioning

• Partition subarray A [ p .. r ] by the following

procedure

Example

• On an 8-element

• subarray

Correctness

  • Use the loop invariant to prove correctness of

PARTITION:

  • Initialization: Before the loop starts, all the conditions of the loop invariant are satisfied, because r is the pivot and the subarrays A [ p .. i ] and A [ i + 1 .. j – 1] are empty.
  • Maintenance: While the loop is running, if A [ j ] ≤ pivot then A [ j ] and A [ i + 1] are swapped and then i and j are incremented. A [ j ] > pivot, then increment only j.
  • Termination : When the loop terminates, j = r , so all elements in A are partitioned into one of the three cases: A [ p .. i ] ≤ pivot, A [ i + 1 .. r - 1] > pivot, and A [ r ] = pivot.

Performance of quicksort

• The running time of quicksort depends on the

partitioning of the subarrays:

  • If the subarrays are balanced, then quicksort can

run as fast as mergesort.

  • If they are unbalanced, then quicksort can run as

slowly as insertion sort.

Worst case

  • Occurs when the subarrays are completely

unbalanced.

  • Have 0 elements in one subarray and n - 1 elements in

the other subarray.

  • Get the recurrence
  • Same running time as insertion sort.
  • In fact, the worst-case running time occurs when quicksort takes a sorted array as input, but insertion sort runs in O( n ) time in this case.

Balanced partitioning

• Quicksort’s average running time is much

closer to the best case than to the worst case.

• Imagine that PARTITION always produces a 9-

to-1 split.

• Get the recurrence

Balanced partitioning

• Intuition: look at the recursion tree

  • It’s like the one for T ( n ) = T ( n / 3) + T (2 n / 3) + O( n )

in Section 4.4.

  • Except that here the constants are different; we

get log 10 n full levels and log 10/9 n levels that are

nonempty.

  • As long as it’s a constant, the base of the log

doesn’t matter in asymptotic notation.

  • Any split of constant proportionality will yield a

recursion tree of depth Θ(lg n ).

Intuition for the average case

Analysis of quicksort

• Worst-case analysis

  • We will prove that a worst-case split at every level

produces a worst-case running time of O ( n^2 ).

  • Recurrence for the worst-case running time of

QUICKSORT:

• Because PARTITION produces two subproblems,

totaling size n - 1, q ranges from 0 to n - 1.

• Guess : T ( n ) ≤ cn^2 for some c.

Worst case analysis