quicksort algorithm
DESCRIPTION
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 - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Quicksort Algorithm](https://reader036.vdocuments.net/reader036/viewer/2022070410/56814625550346895db331b3/html5/thumbnails/1.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 2: Quicksort Algorithm](https://reader036.vdocuments.net/reader036/viewer/2022070410/56814625550346895db331b3/html5/thumbnails/2.jpg)
ExampleWe are given array of n integers to sort:
40 20 10 80 60 50 7 30 100
![Page 3: Quicksort Algorithm](https://reader036.vdocuments.net/reader036/viewer/2022070410/56814625550346895db331b3/html5/thumbnails/3.jpg)
Pick Pivot ElementThere 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 4: Quicksort Algorithm](https://reader036.vdocuments.net/reader036/viewer/2022070410/56814625550346895db331b3/html5/thumbnails/4.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 5: Quicksort Algorithm](https://reader036.vdocuments.net/reader036/viewer/2022070410/56814625550346895db331b3/html5/thumbnails/5.jpg)
40 20 10 80 60 50 7 30 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
![Page 6: Quicksort Algorithm](https://reader036.vdocuments.net/reader036/viewer/2022070410/56814625550346895db331b3/html5/thumbnails/6.jpg)
40 20 10 80 60 50 7 30 100pivot_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 7: Quicksort Algorithm](https://reader036.vdocuments.net/reader036/viewer/2022070410/56814625550346895db331b3/html5/thumbnails/7.jpg)
40 20 10 80 60 50 7 30 100pivot_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 Algorithm](https://reader036.vdocuments.net/reader036/viewer/2022070410/56814625550346895db331b3/html5/thumbnails/8.jpg)
40 20 10 80 60 50 7 30 100pivot_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 Algorithm](https://reader036.vdocuments.net/reader036/viewer/2022070410/56814625550346895db331b3/html5/thumbnails/9.jpg)
40 20 10 80 60 50 7 30 100pivot_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 10: Quicksort Algorithm](https://reader036.vdocuments.net/reader036/viewer/2022070410/56814625550346895db331b3/html5/thumbnails/10.jpg)
40 20 10 80 60 50 7 30 100pivot_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 Algorithm](https://reader036.vdocuments.net/reader036/viewer/2022070410/56814625550346895db331b3/html5/thumbnails/11.jpg)
40 20 10 80 60 50 7 30 100pivot_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_indexswap data[too_big_index] and data[too_small_index]
![Page 12: Quicksort Algorithm](https://reader036.vdocuments.net/reader036/viewer/2022070410/56814625550346895db331b3/html5/thumbnails/12.jpg)
40 20 10 30 60 50 7 80 100pivot_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_indexswap data[too_big_index] and data[too_small_index]
![Page 13: Quicksort Algorithm](https://reader036.vdocuments.net/reader036/viewer/2022070410/56814625550346895db331b3/html5/thumbnails/13.jpg)
40 20 10 30 60 50 7 80 100pivot_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_indexswap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
![Page 14: Quicksort Algorithm](https://reader036.vdocuments.net/reader036/viewer/2022070410/56814625550346895db331b3/html5/thumbnails/14.jpg)
40 20 10 30 60 50 7 80 100pivot_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_indexswap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
![Page 15: Quicksort Algorithm](https://reader036.vdocuments.net/reader036/viewer/2022070410/56814625550346895db331b3/html5/thumbnails/15.jpg)
40 20 10 30 60 50 7 80 100pivot_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_indexswap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
![Page 16: Quicksort Algorithm](https://reader036.vdocuments.net/reader036/viewer/2022070410/56814625550346895db331b3/html5/thumbnails/16.jpg)
40 20 10 30 60 50 7 80 100pivot_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_indexswap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
![Page 17: Quicksort Algorithm](https://reader036.vdocuments.net/reader036/viewer/2022070410/56814625550346895db331b3/html5/thumbnails/17.jpg)
40 20 10 30 60 50 7 80 100pivot_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_indexswap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
![Page 18: Quicksort Algorithm](https://reader036.vdocuments.net/reader036/viewer/2022070410/56814625550346895db331b3/html5/thumbnails/18.jpg)
40 20 10 30 60 50 7 80 100pivot_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_indexswap data[too_big_index] and data[too_small_index]
4. While too_small_index > too_big_index, go to 1.
![Page 19: Quicksort Algorithm](https://reader036.vdocuments.net/reader036/viewer/2022070410/56814625550346895db331b3/html5/thumbnails/19.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_indexswap 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 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
![Page 20: Quicksort Algorithm](https://reader036.vdocuments.net/reader036/viewer/2022070410/56814625550346895db331b3/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_indexswap 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 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
![Page 21: Quicksort Algorithm](https://reader036.vdocuments.net/reader036/viewer/2022070410/56814625550346895db331b3/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_indexswap 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 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
![Page 22: Quicksort Algorithm](https://reader036.vdocuments.net/reader036/viewer/2022070410/56814625550346895db331b3/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_indexswap 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 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
![Page 23: Quicksort Algorithm](https://reader036.vdocuments.net/reader036/viewer/2022070410/56814625550346895db331b3/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_indexswap 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 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
![Page 24: Quicksort Algorithm](https://reader036.vdocuments.net/reader036/viewer/2022070410/56814625550346895db331b3/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_indexswap 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 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
![Page 25: Quicksort Algorithm](https://reader036.vdocuments.net/reader036/viewer/2022070410/56814625550346895db331b3/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_indexswap 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 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
![Page 26: Quicksort Algorithm](https://reader036.vdocuments.net/reader036/viewer/2022070410/56814625550346895db331b3/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_indexswap 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 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
![Page 27: Quicksort Algorithm](https://reader036.vdocuments.net/reader036/viewer/2022070410/56814625550346895db331b3/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_indexswap 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 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
![Page 28: Quicksort Algorithm](https://reader036.vdocuments.net/reader036/viewer/2022070410/56814625550346895db331b3/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_indexswap 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 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
![Page 29: Quicksort Algorithm](https://reader036.vdocuments.net/reader036/viewer/2022070410/56814625550346895db331b3/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_indexswap 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 100pivot_index = 4
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
![Page 30: Quicksort Algorithm](https://reader036.vdocuments.net/reader036/viewer/2022070410/56814625550346895db331b3/html5/thumbnails/30.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 31: Quicksort Algorithm](https://reader036.vdocuments.net/reader036/viewer/2022070410/56814625550346895db331b3/html5/thumbnails/31.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]