quick sort. 2 divide: pick any element p as the pivot, e.g, the first element partition the...
TRANSCRIPT
![Page 1: Quick Sort. 2 Divide: Pick any element p as the pivot, e.g, the first element Partition the remaining elements into FirstPart, which contains all elements](https://reader038.vdocuments.site/reader038/viewer/2022110322/56649d215503460f949f6110/html5/thumbnails/1.jpg)
Quick Sort
![Page 2: Quick Sort. 2 Divide: Pick any element p as the pivot, e.g, the first element Partition the remaining elements into FirstPart, which contains all elements](https://reader038.vdocuments.site/reader038/viewer/2022110322/56649d215503460f949f6110/html5/thumbnails/2.jpg)
2
Quick Sort
• Divide: • Pick any element p as the pivot, e.g, the first element• Partition the remaining elements into
FirstPart, which contains all elements < pSecondPart, which contains all elements ≥ p
• Recursively sort the FirstPart and SecondPart
• Combine: no work is necessary since sorting is done in place
![Page 3: Quick Sort. 2 Divide: Pick any element p as the pivot, e.g, the first element Partition the remaining elements into FirstPart, which contains all elements](https://reader038.vdocuments.site/reader038/viewer/2022110322/56649d215503460f949f6110/html5/thumbnails/3.jpg)
3
Quick Sort
x < p p p ≤ x
PartitionFirstPart SecondPart
ppivot
A:
Recursive call
x < p p p ≤ x
SortedFirstPart
SortedSecondPart
Sorted
![Page 4: Quick Sort. 2 Divide: Pick any element p as the pivot, e.g, the first element Partition the remaining elements into FirstPart, which contains all elements](https://reader038.vdocuments.site/reader038/viewer/2022110322/56649d215503460f949f6110/html5/thumbnails/4.jpg)
4
Quick SortQuick-Sort(A, left, right)if left ≥ right return
else middle ← Partition(A, left,
right) Quick-Sort(A, left, middle–1 ) Quick-Sort(A, middle+1, right)
end if
![Page 5: Quick Sort. 2 Divide: Pick any element p as the pivot, e.g, the first element Partition the remaining elements into FirstPart, which contains all elements](https://reader038.vdocuments.site/reader038/viewer/2022110322/56649d215503460f949f6110/html5/thumbnails/5.jpg)
5
Partitionp
p x < p p ≤ x
p p ≤ xx < p
A:
A:
A:p
![Page 6: Quick Sort. 2 Divide: Pick any element p as the pivot, e.g, the first element Partition the remaining elements into FirstPart, which contains all elements](https://reader038.vdocuments.site/reader038/viewer/2022110322/56649d215503460f949f6110/html5/thumbnails/6.jpg)
6
Partition Example
A: 4 8 6 3 5 1 7 2
![Page 7: Quick Sort. 2 Divide: Pick any element p as the pivot, e.g, the first element Partition the remaining elements into FirstPart, which contains all elements](https://reader038.vdocuments.site/reader038/viewer/2022110322/56649d215503460f949f6110/html5/thumbnails/7.jpg)
7
Partition Example
A: 4 8 6 3 5 1 7 2
i=0
j=1
![Page 8: Quick Sort. 2 Divide: Pick any element p as the pivot, e.g, the first element Partition the remaining elements into FirstPart, which contains all elements](https://reader038.vdocuments.site/reader038/viewer/2022110322/56649d215503460f949f6110/html5/thumbnails/8.jpg)
8
Partition Example
A:
j=1
4 8 6 3 5 1 7 2
i=0
8
![Page 9: Quick Sort. 2 Divide: Pick any element p as the pivot, e.g, the first element Partition the remaining elements into FirstPart, which contains all elements](https://reader038.vdocuments.site/reader038/viewer/2022110322/56649d215503460f949f6110/html5/thumbnails/9.jpg)
9
Partition Example
A: 4 8 6 3 5 1 7 26
i=0
j=2
![Page 10: Quick Sort. 2 Divide: Pick any element p as the pivot, e.g, the first element Partition the remaining elements into FirstPart, which contains all elements](https://reader038.vdocuments.site/reader038/viewer/2022110322/56649d215503460f949f6110/html5/thumbnails/10.jpg)
10
Partition Example
A: 4 8 6 3 5 1 7 2
i=0
383
j=3
i=1
![Page 11: Quick Sort. 2 Divide: Pick any element p as the pivot, e.g, the first element Partition the remaining elements into FirstPart, which contains all elements](https://reader038.vdocuments.site/reader038/viewer/2022110322/56649d215503460f949f6110/html5/thumbnails/11.jpg)
11
Partition Example
A: 4 3 6 8 5 1 7 2
i=1
5
j=4
![Page 12: Quick Sort. 2 Divide: Pick any element p as the pivot, e.g, the first element Partition the remaining elements into FirstPart, which contains all elements](https://reader038.vdocuments.site/reader038/viewer/2022110322/56649d215503460f949f6110/html5/thumbnails/12.jpg)
12
Partition Example
A: 4 3 6 8 5 1 7 2
i=1
1
j=5
![Page 13: Quick Sort. 2 Divide: Pick any element p as the pivot, e.g, the first element Partition the remaining elements into FirstPart, which contains all elements](https://reader038.vdocuments.site/reader038/viewer/2022110322/56649d215503460f949f6110/html5/thumbnails/13.jpg)
13
Partition Example
A: 4 3 6 8 5 1 7 2
i=2
1 6
j=5
![Page 14: Quick Sort. 2 Divide: Pick any element p as the pivot, e.g, the first element Partition the remaining elements into FirstPart, which contains all elements](https://reader038.vdocuments.site/reader038/viewer/2022110322/56649d215503460f949f6110/html5/thumbnails/14.jpg)
14
Partition Example
A: 4 3 8 5 7 2
i=2
1 6 7
j=6
![Page 15: Quick Sort. 2 Divide: Pick any element p as the pivot, e.g, the first element Partition the remaining elements into FirstPart, which contains all elements](https://reader038.vdocuments.site/reader038/viewer/2022110322/56649d215503460f949f6110/html5/thumbnails/15.jpg)
15
Partition Example
A: 4 3 8 5 7 2
i=2
1 6 22 8
i=3
j=7
![Page 16: Quick Sort. 2 Divide: Pick any element p as the pivot, e.g, the first element Partition the remaining elements into FirstPart, which contains all elements](https://reader038.vdocuments.site/reader038/viewer/2022110322/56649d215503460f949f6110/html5/thumbnails/16.jpg)
16
Partition Example
A: 4 3 2 6 7 8
i=3
1 5
j=8
![Page 17: Quick Sort. 2 Divide: Pick any element p as the pivot, e.g, the first element Partition the remaining elements into FirstPart, which contains all elements](https://reader038.vdocuments.site/reader038/viewer/2022110322/56649d215503460f949f6110/html5/thumbnails/17.jpg)
17
Partition Example
A: 4 1 6 7 8
i=3
2 542 3
![Page 18: Quick Sort. 2 Divide: Pick any element p as the pivot, e.g, the first element Partition the remaining elements into FirstPart, which contains all elements](https://reader038.vdocuments.site/reader038/viewer/2022110322/56649d215503460f949f6110/html5/thumbnails/18.jpg)
18
A: 3 6 7 81 542
x < 4 4 ≤ x
pivot incorrect position
Partition Example
![Page 19: Quick Sort. 2 Divide: Pick any element p as the pivot, e.g, the first element Partition the remaining elements into FirstPart, which contains all elements](https://reader038.vdocuments.site/reader038/viewer/2022110322/56649d215503460f949f6110/html5/thumbnails/19.jpg)
19
Partition(A, left, right)1. x ← A[left]2. i ← left3. for j ← left+1 to right4. if A[j] < x then 5. i ← i + 16. swap(A[i], A[j])7. end if8. end for j9. swap(A[i], A[left])10. return in = right – left +1
Time: cn for some constant c Space: constant
![Page 20: Quick Sort. 2 Divide: Pick any element p as the pivot, e.g, the first element Partition the remaining elements into FirstPart, which contains all elements](https://reader038.vdocuments.site/reader038/viewer/2022110322/56649d215503460f949f6110/html5/thumbnails/20.jpg)
20
4 8 6 3 5 1 7 22 3 1 5 6 7 84
Quick-Sort(A, 0, 7)Partition
A:
![Page 21: Quick Sort. 2 Divide: Pick any element p as the pivot, e.g, the first element Partition the remaining elements into FirstPart, which contains all elements](https://reader038.vdocuments.site/reader038/viewer/2022110322/56649d215503460f949f6110/html5/thumbnails/21.jpg)
21
2 3 1
5 6 7 84
2 1 3
Quick-Sort(A, 0, 7)Quick-Sort(A, 0, 2)
A:
, partition
![Page 22: Quick Sort. 2 Divide: Pick any element p as the pivot, e.g, the first element Partition the remaining elements into FirstPart, which contains all elements](https://reader038.vdocuments.site/reader038/viewer/2022110322/56649d215503460f949f6110/html5/thumbnails/22.jpg)
22
2
5 6 7 84
1
1 3
Quick-Sort(A, 0, 7)Quick-Sort(A, 0, 0) , base case, return
![Page 23: Quick Sort. 2 Divide: Pick any element p as the pivot, e.g, the first element Partition the remaining elements into FirstPart, which contains all elements](https://reader038.vdocuments.site/reader038/viewer/2022110322/56649d215503460f949f6110/html5/thumbnails/23.jpg)
23
2
5 6 7 84
1
33
Quick-Sort(A, 0, 7)Quick-Sort(A, 1, 1) , base case
![Page 24: Quick Sort. 2 Divide: Pick any element p as the pivot, e.g, the first element Partition the remaining elements into FirstPart, which contains all elements](https://reader038.vdocuments.site/reader038/viewer/2022110322/56649d215503460f949f6110/html5/thumbnails/24.jpg)
24
5 6 7 842 1 3
2 1 3
Quick-Sort(A, 0, 7)Quick-Sort(A, 2, 2), returnQuick-Sort(A, 0, 2), return
![Page 25: Quick Sort. 2 Divide: Pick any element p as the pivot, e.g, the first element Partition the remaining elements into FirstPart, which contains all elements](https://reader038.vdocuments.site/reader038/viewer/2022110322/56649d215503460f949f6110/html5/thumbnails/25.jpg)
25
42 1 3
5 6 7 86 7 85
Quick-Sort(A, 0, 7)Quick-Sort(A, 2, 2), returnQuick-Sort(A, 4, 7) , partition
![Page 26: Quick Sort. 2 Divide: Pick any element p as the pivot, e.g, the first element Partition the remaining elements into FirstPart, which contains all elements](https://reader038.vdocuments.site/reader038/viewer/2022110322/56649d215503460f949f6110/html5/thumbnails/26.jpg)
26
4
5
6 7 87 866
2 1 3
Quick-Sort(A, 0, 7)Quick-Sort(A, 5, 7) , partition
![Page 27: Quick Sort. 2 Divide: Pick any element p as the pivot, e.g, the first element Partition the remaining elements into FirstPart, which contains all elements](https://reader038.vdocuments.site/reader038/viewer/2022110322/56649d215503460f949f6110/html5/thumbnails/27.jpg)
27
4
5
6
7 887
2 1 3
Quick-Sort(A, 0, 7)Quick-Sort(A, 6, 7) , partition
![Page 28: Quick Sort. 2 Divide: Pick any element p as the pivot, e.g, the first element Partition the remaining elements into FirstPart, which contains all elements](https://reader038.vdocuments.site/reader038/viewer/2022110322/56649d215503460f949f6110/html5/thumbnails/28.jpg)
28
4
5
6
7
2 1 3
Quick-Sort(A, 0, 7)Quick-Sort(A, 7, 7)
8
, return, base case
8
![Page 29: Quick Sort. 2 Divide: Pick any element p as the pivot, e.g, the first element Partition the remaining elements into FirstPart, which contains all elements](https://reader038.vdocuments.site/reader038/viewer/2022110322/56649d215503460f949f6110/html5/thumbnails/29.jpg)
29
4
5
6 87
2 1 3
Quick-Sort(A, 0, 7)Quick-Sort(A, 6, 7) , return
![Page 30: Quick Sort. 2 Divide: Pick any element p as the pivot, e.g, the first element Partition the remaining elements into FirstPart, which contains all elements](https://reader038.vdocuments.site/reader038/viewer/2022110322/56649d215503460f949f6110/html5/thumbnails/30.jpg)
30
4
5
2 1 3
Quick-Sort(A, 0, 7)Quick-Sort(A, 5, 7) , return
6 87
![Page 31: Quick Sort. 2 Divide: Pick any element p as the pivot, e.g, the first element Partition the remaining elements into FirstPart, which contains all elements](https://reader038.vdocuments.site/reader038/viewer/2022110322/56649d215503460f949f6110/html5/thumbnails/31.jpg)
31
42 1 3
Quick-Sort(A, 0, 7)Quick-Sort(A, 4, 7) , return
5 6 87
![Page 32: Quick Sort. 2 Divide: Pick any element p as the pivot, e.g, the first element Partition the remaining elements into FirstPart, which contains all elements](https://reader038.vdocuments.site/reader038/viewer/2022110322/56649d215503460f949f6110/html5/thumbnails/32.jpg)
32
42 1 3
Quick-Sort(A, 0, 7)Quick-Sort(A, 0, 7) , done!
5 6 87