Quicksort:
art of analysis

&

Power of Randomization

MIT OpenCourse

Introduction to Algorithms, Lec 4

 

Tzu-Li Tai (Gordon Tai)

@ NCKU TechOrange

What will be covered

  • Learn the Quicksort algorithm

 

  • Today is going to be all about analysis !!!

 

  • Further learn the Randomized Quicksort

Quicksort

DIVIDE: Divide A[p...r] into left/right partition according to a pivot value, so that elements on the left is smaller than elements on the right. Calculate the index of the pivot.

 

CONQUER: Recursively Quicksort the partitions

The partiTion

Looks hard...right?
Well... it's actually super easy

Try to love math

WHY?

Lecture 4 allowed me to realize

how fun algorithms can be when

combined with mathematics !!!

Where it starts to get fun:

Performance analysis

  • Worst case

 

  • Best case

 

  • 亂七八糟 case

亂中有序 -

Randomized quicksort

Randomly select the pivot element (instead of always using the last element as pivot).

 

Remember to swap the chosen pivot with last element.

What's good about randomized quicksort?

  • Independent of input ordering

 

  • You might ask: what if we still always pick a bad pivot?

 

  • That's caused by the random-number generator, not the input.

iT'S rANDOM.

wHAT CAN WE "EXPECT"?

What we did today...

  • Learn a new sorting algorithm

 

  • More importantly, we analyzed the algorithm

 

  • We found out that the algorithm performs very bad on worst cases. However, it perform very well on some other cases.

What we did today...

  • Used a "randomization" technique to eliminate dependency on algorithm input.

 

  • By doing so, we can expect the performance of the randomized algorithm, no matter what the input is!

 

  • This, is the power of randomized algorithms

EXTRA:

Designing your own algorithm -

Walking a maze

Quicksort - The Art of Algorithm Analysis

By Tzu-Li Tai

Quicksort - The Art of Algorithm Analysis

MIT OpenCourse, Introduction to Algorithms, Lec 4

  • 507