Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4....
Transcript of Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4....
![Page 1: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/1.jpg)
Quicksort
![Page 2: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/2.jpg)
Quicksort Algorithm
Given an array of n elements (e.g., integers): • If array only contains one element, return • Else
– pick one element to use as pivot. – Partition elements into two sub-arrays:
• Elements less than or equal to pivot • Elements greater than pivot
– Quicksort two sub-arrays – Return results
![Page 3: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/3.jpg)
Example We are given array of n integers to sort:
40 20 10 80 60 50 7 30 100
![Page 4: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/4.jpg)
Pick Pivot Element There are a number of ways to pick the pivot element. In this
example, we will use the first element in the array:
40 20 10 80 60 50 7 30 100
![Page 5: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/5.jpg)
Partitioning Array
Given a pivot, partition the elements of the array such that the resulting array consists of:
1. One sub-array that contains elements >= pivot 2. Another sub-array that contains elements < pivot
The sub-arrays are stored in the original data array. Partitioning loops through, swapping elements
below/above pivot.
![Page 6: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/6.jpg)
40 20 10 80 60 50 7 30 100 pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
![Page 7: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/7.jpg)
40 20 10 80 60 50 7 30 100 pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot] ++too_big_index
![Page 8: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/8.jpg)
40 20 10 80 60 50 7 30 100 pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot] ++too_big_index
![Page 9: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/9.jpg)
40 20 10 80 60 50 7 30 100 pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot] ++too_big_index
![Page 10: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/10.jpg)
40 20 10 80 60 50 7 30 100 pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot] ++too_big_index
2. While data[too_small_index] > data[pivot] --too_small_index
![Page 11: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/11.jpg)
40 20 10 80 60 50 7 30 100 pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot] ++too_big_index
2. While data[too_small_index] > data[pivot] --too_small_index
![Page 12: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/12.jpg)
40 20 10 80 60 50 7 30 100 pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot] ++too_big_index
2. While data[too_small_index] > data[pivot] --too_small_index
3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]
![Page 13: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/13.jpg)
40 20 10 30 60 50 7 80 100 pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot] ++too_big_index
2. While data[too_small_index] > data[pivot] --too_small_index
3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]
![Page 14: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/14.jpg)
40 20 10 30 60 50 7 80 100 pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot] ++too_big_index
2. While data[too_small_index] > data[pivot] --too_small_index
3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
![Page 15: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/15.jpg)
40 20 10 30 60 50 7 80 100 pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot] ++too_big_index
2. While data[too_small_index] > data[pivot] --too_small_index
3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
![Page 16: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/16.jpg)
40 20 10 30 60 50 7 80 100 pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot] ++too_big_index
2. While data[too_small_index] > data[pivot] --too_small_index
3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
![Page 17: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/17.jpg)
40 20 10 30 60 50 7 80 100 pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot] ++too_big_index
2. While data[too_small_index] > data[pivot] --too_small_index
3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
![Page 18: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/18.jpg)
40 20 10 30 60 50 7 80 100 pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot] ++too_big_index
2. While data[too_small_index] > data[pivot] --too_small_index
3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
![Page 19: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/19.jpg)
40 20 10 30 60 50 7 80 100 pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
1. While data[too_big_index] <= data[pivot] ++too_big_index
2. While data[too_small_index] > data[pivot] --too_small_index
3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
![Page 20: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/20.jpg)
1. While data[too_big_index] <= data[pivot] ++too_big_index
2. While data[too_small_index] > data[pivot] --too_small_index
3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
40 20 10 30 7 50 60 80 100 pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
![Page 21: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/21.jpg)
1. While data[too_big_index] <= data[pivot] ++too_big_index
2. While data[too_small_index] > data[pivot] --too_small_index
3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
40 20 10 30 7 50 60 80 100 pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
![Page 22: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/22.jpg)
1. While data[too_big_index] <= data[pivot] ++too_big_index
2. While data[too_small_index] > data[pivot] --too_small_index
3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
40 20 10 30 7 50 60 80 100 pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
![Page 23: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/23.jpg)
1. While data[too_big_index] <= data[pivot] ++too_big_index
2. While data[too_small_index] > data[pivot] --too_small_index
3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
40 20 10 30 7 50 60 80 100 pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
![Page 24: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/24.jpg)
1. While data[too_big_index] <= data[pivot] ++too_big_index
2. While data[too_small_index] > data[pivot] --too_small_index
3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
40 20 10 30 7 50 60 80 100 pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
![Page 25: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/25.jpg)
1. While data[too_big_index] <= data[pivot] ++too_big_index
2. While data[too_small_index] > data[pivot] --too_small_index
3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
40 20 10 30 7 50 60 80 100 pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
![Page 26: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/26.jpg)
1. While data[too_big_index] <= data[pivot] ++too_big_index
2. While data[too_small_index] > data[pivot] --too_small_index
3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
40 20 10 30 7 50 60 80 100 pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
![Page 27: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/27.jpg)
1. While data[too_big_index] <= data[pivot] ++too_big_index
2. While data[too_small_index] > data[pivot] --too_small_index
3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
40 20 10 30 7 50 60 80 100 pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
![Page 28: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/28.jpg)
1. While data[too_big_index] <= data[pivot] ++too_big_index
2. While data[too_small_index] > data[pivot] --too_small_index
3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
40 20 10 30 7 50 60 80 100 pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
![Page 29: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/29.jpg)
1. While data[too_big_index] <= data[pivot] ++too_big_index
2. While data[too_small_index] > data[pivot] --too_small_index
3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1. 5. Swap data[too_small_index] and data[pivot_index]
40 20 10 30 7 50 60 80 100 pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
![Page 30: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/30.jpg)
1. While data[too_big_index] <= data[pivot] ++too_big_index
2. While data[too_small_index] > data[pivot] --too_small_index
3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1. 5. Swap data[too_small_index] and data[pivot_index]
7 20 10 30 40 50 60 80 100 pivot_index = 4
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
![Page 31: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/31.jpg)
Partition Result
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
<= data[pivot] > data[pivot]
![Page 32: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/32.jpg)
Recursion: Quicksort Sub-arrays
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
<= data[pivot] > data[pivot]
![Page 33: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/33.jpg)
Quicksort Analysis
• Assume that keys are random, uniformly distributed.
• What is best case running time?
![Page 34: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/34.jpg)
Quicksort Analysis
• Assume that keys are random, uniformly distributed.
• What is best case running time? – Recursion:
1. Partition splits array in two sub-arrays of size n/2 2. Quicksort each sub-array
![Page 35: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/35.jpg)
Quicksort Analysis
• Assume that keys are random, uniformly distributed.
• What is best case running time? – Recursion:
1. Partition splits array in two sub-arrays of size n/2 2. Quicksort each sub-array
– Depth of recursion tree?
![Page 36: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/36.jpg)
Quicksort Analysis
• Assume that keys are random, uniformly distributed.
• What is best case running time? – Recursion:
1. Partition splits array in two sub-arrays of size n/2 2. Quicksort each sub-array
– Depth of recursion tree? O(log2n)
![Page 37: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/37.jpg)
Quicksort Analysis
• Assume that keys are random, uniformly distributed.
• What is best case running time? – Recursion:
1. Partition splits array in two sub-arrays of size n/2 2. Quicksort each sub-array
– Depth of recursion tree? O(log2n) – Number of accesses in partition?
![Page 38: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/38.jpg)
Quicksort Analysis
• Assume that keys are random, uniformly distributed.
• What is best case running time? – Recursion:
1. Partition splits array in two sub-arrays of size n/2 2. Quicksort each sub-array
– Depth of recursion tree? O(log2n) – Number of accesses in partition? O(n)
![Page 39: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/39.jpg)
Quicksort Analysis
• Assume that keys are random, uniformly distributed.
• Best case running time: O(n log2n)
![Page 40: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/40.jpg)
Quicksort Analysis
• Assume that keys are random, uniformly distributed.
• Best case running time: O(n log2n) • Worst case running time?
![Page 41: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/41.jpg)
Quicksort: Worst Case
• Assume first element is chosen as pivot. • Assume we get array that is already in
order: 2 4 10 12 13 50 57 63 100 pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
![Page 42: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/42.jpg)
1. While data[too_big_index] <= data[pivot] ++too_big_index
2. While data[too_small_index] > data[pivot] --too_small_index
3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1. 5. Swap data[too_small_index] and data[pivot_index]
2 4 10 12 13 50 57 63 100 pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
![Page 43: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/43.jpg)
1. While data[too_big_index] <= data[pivot] ++too_big_index
2. While data[too_small_index] > data[pivot] --too_small_index
3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1. 5. Swap data[too_small_index] and data[pivot_index]
2 4 10 12 13 50 57 63 100 pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
![Page 44: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/44.jpg)
1. While data[too_big_index] <= data[pivot] ++too_big_index
2. While data[too_small_index] > data[pivot] --too_small_index
3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1. 5. Swap data[too_small_index] and data[pivot_index]
2 4 10 12 13 50 57 63 100 pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
![Page 45: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/45.jpg)
1. While data[too_big_index] <= data[pivot] ++too_big_index
2. While data[too_small_index] > data[pivot] --too_small_index
3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1. 5. Swap data[too_small_index] and data[pivot_index]
2 4 10 12 13 50 57 63 100 pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
![Page 46: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/46.jpg)
1. While data[too_big_index] <= data[pivot] ++too_big_index
2. While data[too_small_index] > data[pivot] --too_small_index
3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1. 5. Swap data[too_small_index] and data[pivot_index]
2 4 10 12 13 50 57 63 100 pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
![Page 47: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/47.jpg)
1. While data[too_big_index] <= data[pivot] ++too_big_index
2. While data[too_small_index] > data[pivot] --too_small_index
3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1. 5. Swap data[too_small_index] and data[pivot_index]
2 4 10 12 13 50 57 63 100 pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
![Page 48: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/48.jpg)
1. While data[too_big_index] <= data[pivot] ++too_big_index
2. While data[too_small_index] > data[pivot] --too_small_index
3. If too_big_index < too_small_index swap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1. 5. Swap data[too_small_index] and data[pivot_index]
2 4 10 12 13 50 57 63 100 pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
> data[pivot] <= data[pivot]
![Page 49: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/49.jpg)
Quicksort Analysis
• Assume that keys are random, uniformly distributed.
• Best case running time: O(n log2n) • Worst case running time?
– Recursion: 1. Partition splits array in two sub-arrays:
• one sub-array of size 0 • the other sub-array of size n-1
2. Quicksort each sub-array – Depth of recursion tree?
![Page 50: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/50.jpg)
Quicksort Analysis
• Assume that keys are random, uniformly distributed.
• Best case running time: O(n log2n) • Worst case running time?
– Recursion: 1. Partition splits array in two sub-arrays:
• one sub-array of size 0 • the other sub-array of size n-1
2. Quicksort each sub-array – Depth of recursion tree? O(n)
![Page 51: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/51.jpg)
Quicksort Analysis
• Assume that keys are random, uniformly distributed.
• Best case running time: O(n log2n) • Worst case running time?
– Recursion: 1. Partition splits array in two sub-arrays:
• one sub-array of size 0 • the other sub-array of size n-1
2. Quicksort each sub-array – Depth of recursion tree? O(n) – Number of accesses per partition?
![Page 52: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/52.jpg)
Quicksort Analysis
• Assume that keys are random, uniformly distributed.
• Best case running time: O(n log2n) • Worst case running time?
– Recursion: 1. Partition splits array in two sub-arrays:
• one sub-array of size 0 • the other sub-array of size n-1
2. Quicksort each sub-array – Depth of recursion tree? O(n) – Number of accesses per partition? O(n)
![Page 53: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/53.jpg)
Quicksort Analysis
• Assume that keys are random, uniformly distributed.
• Best case running time: O(n log2n) • Worst case running time: O(n2)!!!
![Page 54: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/54.jpg)
Quicksort Analysis
• Assume that keys are random, uniformly distributed.
• Best case running time: O(n log2n) • Worst case running time: O(n2)!!! • What can we do to avoid worst case?
![Page 55: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/55.jpg)
Improved Pivot Selection
Pick median value of three elements from data array: data[0], data[n/2], and data[n-1]. Use this median value as pivot.
![Page 56: Quicksort - DISI, University of Trentodisi.unitn.it/~sebe/info/12b-quicksort.pdf · 2018. 4. 27. · Quicksort Algorithm Given an array of n elements (e.g., integers): • If array](https://reader036.fdocuments.net/reader036/viewer/2022081407/604ce9f3460e9b1cab20f43a/html5/thumbnails/56.jpg)
Improving Performance of Quicksort
• Improved selection of pivot. • For sub-arrays of size 3 or less, apply brute
force search: – Sub-array of size 1: trivial – Sub-array of size 2:
• if(data[first] > data[second]) swap them – Sub-array of size 3: left as an exercise.