top 10 algorithms in the 20 century quicksortweb.math.ucsb.edu/~siam/2014-fall/li_quicksort.pdf ·...

67
Top 10 Algorithms in the 20 th century QuickSort Shunyao Li 10/22/2014

Upload: others

Post on 22-May-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Top 10 Algorithms in the 20th century

QuickSortShunyao Li

10/22/2014

Page 2: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Outline

u Introduction

u QuickSort

u Complexity Analysis

u Optimization

u Application

Page 3: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Outline

u Introduction

u QuickSort

u Complexity Analysis

u Optimization

u Application

Page 4: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

What is sorting

Page 5: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

What is sorting

Page 6: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

History

Page 7: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

History

Insertion Sort

Page 8: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

History

Insertion SortSelection Sort

Page 9: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

History

Insertion SortSelection Sort

1948 Merge Sort

1956 Bubble Sort

1960 Quick Sort

1964 Heap Sort

21st century Hybrid Sorting algorithms

most widely used

Page 10: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Concepts

u Time Complexity

u number of comparisons

u Space Complexity function of n (scale of the problem) best/worst/average cases

u Big O Notation n: scale of the problem

O(n2)O(n) O(logn)

O(f(n)) = cf(n)

Page 11: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

O(f(n)) = cf(n)

f(n) = n, c = 1

f(n) = log(n), c = 1

f(n) = nlog(n), c = 1

f(n) = n2, c = 1

n

Complexity O(f(n))

Page 12: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

n

f(n) = n2, c = 1

f(n) = nlog(n), c = 1000

O(f(n)) = cf(n)

n

f(n) = nlog(n), c = 500

Complexity O(f(n))

Page 13: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Divide and Conquer

u Divide a big problem into several small problems

. . .

Eg: Binary Search, Fast Fourier Transform…

Page 14: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Outline

u Introduction

u QuickSort

u Complexity Analysis

u Optimization

u Application

Page 15: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Quick Sort

21

1289

5

1810

Page 16: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Quick Sort

21

1289

5

1810

Page 17: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Quick Sort

21

12

8 9

5

1810

Page 18: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Quick Sort

21

12

8 95

1810

Page 19: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Quick Sort

u Step One: Select the first element, called pivot

PivotValues less than Pivot Values greater than Pivot

u Step Three: Apply the above steps over and over again on each sub-array until there is only one element in the sub-array.

Sub-array A Sub-array B

u Step Two: Re-order the array so that all elements less than pivot come before the pivot, values greater than pivot come after it. How to re-order the array?

Page 20: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Quick Sort Example

23 10 49 34 12 19 24

pivot

Select the first element as Pivot

Page 21: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Quick Sort Example

2310 49 34 12 19 24

pivotSelect the first element as Pivot

Page 22: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Quick Sort Example

10 49 34 12 19 24

PL points to the element on the left; PR points to the element on the right

PL

PR23

pivot

Page 23: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Quick Sort Example

10 49 34 12 19 24

If the element in PL is less than pivot, pass it

PL

PR23

pivot

Page 24: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Quick Sort Example

10 49 34 12 19 2423

pivot

PL

PR

If the element in PL is greater than pivot, stop

Page 25: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Quick Sort Example

10 49 34 12 19 2423

pivot

PL

PR

If the element in PR is greater than pivot, pass it

Page 26: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Quick Sort Example

10 49 34 12 19 2423

pivot

PL

PR

If the element in PR is less than pivot, stop

Page 27: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Quick Sort Example

10 49 34 12 19 2423

pivot

PL

PR

Swap

Page 28: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Quick Sort Example

10 19 34 12 49 2423

pivot

PL

PR

Continue Moving

Page 29: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Quick Sort Example

10 19 34 12 49 2423

pivot

PL

PR

Swap

Page 30: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Quick Sort Example

10 19 12 34 49 2423

pivot

PL

PR

Continue Moving until PL is behind PR Have scanned over the whole sequence

Page 31: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Quick Sort Example

10 19 12 34 49 2423

pivot

PL

PR

swap pivot with PR

Page 32: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Quick Sort Example

10 19 23 34 49 2412

pivot

Left sub-array Right sub-array

Page 33: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Quick Sort Example

10 19 23 34 49 2412

12 1910 24 34 49

Page 34: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Quick Sort Example

10 19 23 34 49 2312

24 34 4912 1910

10 19 24 49

Page 35: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Quick Sort Example

10 19 23 34 49 2312

12 1910 24 34 49

10 19 24 49

12 23 24 4919 3410

Page 36: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Outline

u Introduction

u QuickSort

u Complexity Analysis

u Optimization

u Application

Page 37: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Complexity Analysis

37

u Time/Space  Complexity    u for  best/worst/average  cases

Time Space

Best

Worst

Average

Page 38: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Best Case

0 comparison

n-1=6 comparisons

n-3=2+2 comparisons

6+(2+2)=10 comparisons

length of the array: n=7

Depth=3

Level=1

Level=2

Level=3

10 19 23 34 49 2312

12 1910 24 34 49

10 19 24 49

12 23 24 4919 3410

Time Space

Best

Worst

Average

Tree

Page 39: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Best Caselength of the array: n=7

10 19 23 34 49 2312

12 1910 24 34 49

10 19 24 49

Time Space

Best

Worst

Average

n comparison

n comparisons

n comparisons

Minimum Depth = log(n) (See reference)Depth=3

Level=1

Level=2

Level=3

12 23 24 4919 3410

6+(2+2)=10 comparisons

Page 40: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Best Caselength of the array: n=7

10 19 23 34 49 2312

12 1910 24 34 49

10 19 24 49

Time Space

Best

Worst

Average

n comparison

n comparisons

n comparisons

u Total  comparisons  =  n  *  Depth  =  nlog(n)  u Memory  usage  =  Depth  =  log(n)  u Time  Complexity:  O(nlog(n))  u Space  Complexity:  O(logn)

Minimum Depth = log(n) (See reference)Depth=3

Level=1

Level=2

Level=3

Page 41: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Best Caselength of the array: n=7

10 19 23 34 49 2312

12 1910 24 34 49

10 19 24 49 n comparison

n comparisons

n comparisons

u Total  comparisons  =  n  *  Depth  =  nlog(n)  u Memory  usage  =  Depth  =  log(n)  u Time  Complexity:  O(nlog(n))  u Space  Complexity:  O(logn)

Minimum Depth = log(n) (See reference)Depth=3

Level=1

Level=2

Level=3

Time Space

Best O(nlogn) O(logn)

Worst

Average

Page 42: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Worst Case

Time Space

Best O(nlogn) O(logn)

Worst

Average

Maximize Depth

Page 43: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Worst Case 10121923243449

Time Space

Best O(nlogn) O(logn)

Worst

Average

Maximize Depth

10 12 19 23 24 34 49

Page 44: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Worst Case

10 12 19 23 24 34 49pivot

49

12 19 23 24 34 49

19 23 24 34 49

23 24 34 49

24 34 49

34 49

Time Space

Best O(nlogn) O(logn)

Worst

Average

Page 45: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Worst Case

10 12 19 23 24 34 49

49

12 19 23 24 34 49

19 23 24 34 49

23 24 34 49

24 34 49

34 49

6 comparisons

5 comparisons

4 comparisons

3 comparisons

2 comparisons

1 comparison

(n� 1) + (n� 2) + ...+ 1 = O(n2)

Time Space

Best O(nlogn) O(logn)

Worst

Average

Depth=n=7

Level=1

Level=2

Level=3

Level=4

Level=5

Level=6

Level=7

length of the array: n=7

Page 46: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Worst Case

10 12 19 23 24 34 49

49

12 19 23 24 34 49

19 23 24 34 49

23 24 34 49

24 34 49

34 49

6 comparisons

5 comparisons

4 comparisons

3 comparisons

2 comparisons

1 comparison

(n� 1) + (n� 2) + ...+ 1 = O(n2)

Time Space

Best O(nlogn) O(logn)

Worst

Average

Depth=n=7

Level=1

Level=2

Level=3

Level=4

Level=5

Level=6

Level=7u Time  Complexity=  n  *  Depth  =  O(n*n)  u Space  Complexity=  Depth  =  O(n)

length of the array: n=7

Page 47: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Worst Case

10 12 19 23 24 34 49

49

12 19 23 24 34 49

19 23 24 34 49

23 24 34 49

24 34 49

34 49

6 comparisons

5 comparisons

4 comparisons

3 comparisons

2 comparisons

1 comparison

(n� 1) + (n� 2) + ...+ 1 = O(n2)

Depth=n=7

Level=1

Level=2

Level=3

Level=4

Level=5

Level=6

Level=7u Time  Complexity=  n  *  Depth  =  O(n*n)  u Space  Complexity=  Depth  =  O(n)

Time Space

Best O(nlogn) O(logn)

Worst O(n)

Average

O(n2)

length of the array: n=7

Page 48: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Average Case

Left sub-array Right sub-array

i: pivot partition location

By Mathematical induction

T (n) = O(nlog(n))Time Space

Best O(nlogn) O(logn)

Worst O(n)

Average

O(n2)

Prob(i) #comparisons at each level

Page 49: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Average Case

Left sub-array Right sub-array

i: pivot partition location

By Mathematical induction

T (n) = O(nlog(n))O(n2)

Prob(i) #comparisons at each level

Time Space

Best O(nlogn) O(logn)

Worst O(n)

Average O(nlogn) O(logn)

O(n2)

Page 50: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Complexity Summary

Time Space

Best

Worst

Average

O(n2)

O(logn)

O(logn)

O(nlogn)

O(nlogn)

O(n)

Page 51: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Sorting Algorithm Comparison

Best Average Worst

Insertion Sort

Selection Sort

Bubble Sort

Merge Sort

Heap Sort

Quick Sort

Page 52: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Sorting Algorithm Comparison

Best Average Worst

Insertion Sort nSelection Sort

Bubble Sort nMerge Sort

Heap Sort

Quick Sort nlogn

n2

Page 53: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Sorting Algorithm Comparison

Best Average Worst

Insertion Sort nSelection Sort

Bubble Sort nMerge Sort

Heap Sort

Quick Sort nlogn

n2

n2

n2

n2

n2

Page 54: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Sorting Algorithm Comparison

Best Average Worst

Insertion Sort nSelection Sort

Bubble Sort nMerge Sort

Heap Sort

Quick Sort nlogn nlogn

n2

n2

n2

n2

n2

n2

n2

n2

Page 55: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Sorting Algorithm Comparison

Best Average Worst

Insertion Sort nSelection Sort

Bubble Sort nMerge Sort nlogn nlogn nlognHeap Sort nlogn nlogn nlognQuick Sort nlogn nlogn

n2

n2

n2

n2

n2

n2

n2

*MergeSort  and  HeapSort  are  faster  than  QuickSort  in  the  worst  case.  

n2

Page 56: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Sorting Algorithm Comparison

Best Average Worst

Insertion Sort nSelection Sort

Bubble Sort nMerge Sort nlogn nlogn nlognHeap Sort nlogn nlogn nlognQuick Sort nlogn nlogn

n2

n2

n2

n2

n2

n2

n2

*MergeSort  and  HeapSort  are  faster  than  QuickSort  in  the  worst  case.  

n2

O(f(n)) = cf(n)

*Practically,  QuickSort  is  still  the  fastest:  constant  factor  on  average  case  is  small

Can  it  be  even  better?

Page 57: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Outline

u Introduction

u QuickSort

u Complexity Analysis

u Optimization

u Application

Page 58: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Optimization

Time Space

Best

Worst

Average

O(n2)

O(logn)

O(logn)

O(nlogn)

O(nlogn)

O(n)

Page 59: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Avoid the worst case

u Choose  pivot  randomly  u Choose  pivot  as  a  “median-­‐of-­‐three”  u Use  insertion  sort  for  small  arrays  u Choose  median  of  the  array  as  pivot  

u always  be  O(nlog(n))  u Need  O(n)  for  finding  the  median  u rarely  used  because  of  the  large  algorithm  

complexity  and  the  very  large  constant  factor  on  O(n)

Used in <stdlib> of C

pivot/partition

Page 60: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Can we come up with an even better solution?

u There  is  a  lower  bound  in  time  complexity

Page 61: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Lower Bound in Time Complexity

u Think of sorting as a decision tree: (eg: {1,2,3} )

61

u Each leaf is a possible order of {1,2,3}

u n numbers -> n! possibilities -> n! leaves

u Fast -> make less decisions -> less levels -> less depth

u minimum depth = log(n!) ~= O(nlogn)

1 < 2 ?

2 < 3 ? 1 < 3 ?

1 < 3 ? 2 < 3 ?1 2 3

1 3 2 3 1 2

2 1 3

2 3 1 3 2 1

Y

Y

Y

Y

Y

N

N N

N Ndepth

Page 62: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Outline

u Introduction

u QuickSort

u Complexity Analysis

u Optimization

u Application

Page 63: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Application

u C  standard  library  u http://repo.or.cz/w/glibc.git/blob/HEAD:/stdlib/qsort.c  u median  of  three  u Use  insertion  for  small  arrays  

u Java:  Dual-­‐pivot  quicksort

P1 <P1 P1<=X<=P2 >P2 P2left rightL G

part I part IIpart III

u The  Kth  largest  numberpivot

t < K

Page 64: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Summary

u QuickSort  is  an  divide-­‐and-­‐conquer  based  algorithm  

u QuickSort  can  be  summarized  as  two  steps:  u Select  and  place  the  pivot  in  its  correct  place  u Recursively  sort  both  sides  of  the  pivot  

u Optimization  and  Application

Time Space

Best

WorstAverag

e

O(n2)

O(logn)

O(logn)

O(nlogn)

O(nlogn)

O(n)

Page 65: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Interesting Visualization

u https://www.youtube.com/watch?v=kPRA0W1kECg

Page 66: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Reference

u Proof for the depth of a binary tree: http://cs.stackexchange.com/questions/6161/what-is-the-depth-of-a-complete-binary-tree-with-n-nodes

u Proof for the best case: http://www.cs.princeton.edu/courses/archive/spr03/cs226/lectures/analysis.4up.pdf

u Discussion of Multi-Pivot QuickSort: http://cs.stanford.edu/~rishig/courses/ref/l11a.pdf

u Proof for average case of QuickSort: http://en.wikipedia.org/wiki/Quicksort

66

Page 67: Top 10 Algorithms in the 20 century QuickSortweb.math.ucsb.edu/~siam/2014-Fall/li_quicksort.pdf · History Insertion Sort Selection Sort 1948 Merge Sort 1956 Bubble Sort 1960 Quick

Thanks!

67