algorithmsyoshi/sedgewick/algs4th/slides/...lt i gt less unknown equal ・let v be partitioning item...
TRANSCRIPT
![Page 1: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/1.jpg)
ROBERT SEDGEWICK | KEVIN WAYNE
F O U R T H E D I T I O N
Algorithms
http://algs4.cs.princeton.edu
Algorithms ROBERT SEDGEWICK | KEVIN WAYNE
2.3 PARTITIONING DEMOS
‣ Sedgewick 2-way partitioning
‣ Dijkstra 3-way partitioning
‣ Bentley-McIlroy 3-way partitioning
‣ dual-pivot partitioning
![Page 2: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/2.jpg)
http://algs4.cs.princeton.edu
ROBERT SEDGEWICK | KEVIN WAYNE
Algorithms
2.3 PARTITIONING DEMOS
‣ Sedgewick 2-way partitioning
‣ Dijkstra 3-way partitioning
‣ Bentley-McIlroy 3-way partitioning
‣ dual-pivot partitioning
![Page 3: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/3.jpg)
Quicksort partitioning demo
Repeat until i and j pointers cross.
・Scan i from left to right so long as (a[i] < a[lo]).
・Scan j from right to left so long as (a[j] > a[lo]).
・Exchange a[i] with a[j].
lo
K R A T E L E P U I M Q C X O S
i j
stop i scan because a[i] >= a[lo]
![Page 4: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/4.jpg)
Quicksort partitioning demo
Repeat until i and j pointers cross.
・Scan i from left to right so long as (a[i] < a[lo]).
・Scan j from right to left so long as (a[j] > a[lo]).
・Exchange a[i] with a[j].
lo
K R A T E L E P U I M Q C X O S
i j
![Page 5: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/5.jpg)
Quicksort partitioning demo
Repeat until i and j pointers cross.
・Scan i from left to right so long as (a[i] < a[lo]).
・Scan j from right to left so long as (a[j] > a[lo]).
・Exchange a[i] with a[j].
lo
K R A T E L E P U I M Q C X O S
i j
![Page 6: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/6.jpg)
Quicksort partitioning demo
Repeat until i and j pointers cross.
・Scan i from left to right so long as (a[i] < a[lo]).
・Scan j from right to left so long as (a[j] > a[lo]).
・Exchange a[i] with a[j].
lo
K R A T E L E P U I M Q C X O S
i j
stop j scan and exchange a[i] with a[j]
![Page 7: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/7.jpg)
Quicksort partitioning demo
Repeat until i and j pointers cross.
・Scan i from left to right so long as (a[i] < a[lo]).
・Scan j from right to left so long as (a[j] > a[lo]).
・Exchange a[i] with a[j].
lo
K C A T E L E P U I M Q R X O S
i j
![Page 8: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/8.jpg)
Quicksort partitioning demo
Repeat until i and j pointers cross.
・Scan i from left to right so long as (a[i] < a[lo]).
・Scan j from right to left so long as (a[j] > a[lo]).
・Exchange a[i] with a[j].
lo
K C A T E L E P U I M Q R X O S
i j
![Page 9: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/9.jpg)
Quicksort partitioning demo
Repeat until i and j pointers cross.
・Scan i from left to right so long as (a[i] < a[lo]).
・Scan j from right to left so long as (a[j] > a[lo]).
・Exchange a[i] with a[j].
lo
K C A T E L E P U I M Q R X O S
i j
stop i scan because a[i] >= a[lo]
![Page 10: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/10.jpg)
Quicksort partitioning demo
Repeat until i and j pointers cross.
・Scan i from left to right so long as (a[i] < a[lo]).
・Scan j from right to left so long as (a[j] > a[lo]).
・Exchange a[i] with a[j].
lo
K C A T E L E P U I M Q R X O S
i j
![Page 11: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/11.jpg)
Quicksort partitioning demo
Repeat until i and j pointers cross.
・Scan i from left to right so long as (a[i] < a[lo]).
・Scan j from right to left so long as (a[j] > a[lo]).
・Exchange a[i] with a[j].
lo
K C A T E L E P U I M Q R X O S
i j
![Page 12: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/12.jpg)
Quicksort partitioning demo
Repeat until i and j pointers cross.
・Scan i from left to right so long as (a[i] < a[lo]).
・Scan j from right to left so long as (a[j] > a[lo]).
・Exchange a[i] with a[j].
lo
K C A T E L E P U I M Q R X O S
i j
stop j scan and exchange a[i] with a[j]
![Page 13: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/13.jpg)
Quicksort partitioning demo
Repeat until i and j pointers cross.
・Scan i from left to right so long as (a[i] < a[lo]).
・Scan j from right to left so long as (a[j] > a[lo]).
・Exchange a[i] with a[j].
lo
K C A I E L E P U T M Q R X O S
i j
![Page 14: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/14.jpg)
Quicksort partitioning demo
Repeat until i and j pointers cross.
・Scan i from left to right so long as (a[i] < a[lo]).
・Scan j from right to left so long as (a[j] > a[lo]).
・Exchange a[i] with a[j].
lo
K C A I E L E P U T M Q R X O S
i j
![Page 15: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/15.jpg)
Quicksort partitioning demo
Repeat until i and j pointers cross.
・Scan i from left to right so long as (a[i] < a[lo]).
・Scan j from right to left so long as (a[j] > a[lo]).
・Exchange a[i] with a[j].
lo
K C A I E L E P U T M Q R X O S
i j
stop i scan because a[i] >= a[lo]
![Page 16: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/16.jpg)
Quicksort partitioning demo
Repeat until i and j pointers cross.
・Scan i from left to right so long as (a[i] < a[lo]).
・Scan j from right to left so long as (a[j] > a[lo]).
・Exchange a[i] with a[j].
lo
K C A I E L E P U T M Q R X O S
i j
![Page 17: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/17.jpg)
Quicksort partitioning demo
Repeat until i and j pointers cross.
・Scan i from left to right so long as (a[i] < a[lo]).
・Scan j from right to left so long as (a[j] > a[lo]).
・Exchange a[i] with a[j].
lo
K C A I E L E P U T M Q R X O S
i j
![Page 18: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/18.jpg)
Quicksort partitioning demo
Repeat until i and j pointers cross.
・Scan i from left to right so long as (a[i] < a[lo]).
・Scan j from right to left so long as (a[j] > a[lo]).
・Exchange a[i] with a[j].
lo
K C A I E L E P U T M Q R X O S
i j
stop j scan and exchange a[i] with a[j]
![Page 19: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/19.jpg)
Quicksort partitioning demo
Repeat until i and j pointers cross.
・Scan i from left to right so long as (a[i] < a[lo]).
・Scan j from right to left so long as (a[j] > a[lo]).
・Exchange a[i] with a[j].
lo
K C A I E E L P U T M Q R X O S
i j
![Page 20: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/20.jpg)
Quicksort partitioning demo
Repeat until i and j pointers cross.
・Scan i from left to right so long as (a[i] < a[lo]).
・Scan j from right to left so long as (a[j] > a[lo]).
・Exchange a[i] with a[j].
lo
K C A I E E L P U T M Q R X O S
i j
stop i scan because a[i] >= a[lo]
![Page 21: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/21.jpg)
Quicksort partitioning demo
Repeat until i and j pointers cross.
・Scan i from left to right so long as (a[i] < a[lo]).
・Scan j from right to left so long as (a[j] > a[lo]).
・Exchange a[i] with a[j].
lo
K C A I E E L P U T M Q R X O S
ij
stop j scan because a[j] <= a[lo]
![Page 22: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/22.jpg)
Quicksort partitioning demo
Repeat until i and j pointers cross.
・Scan i from left to right so long as (a[i] < a[lo]).
・Scan j from right to left so long as (a[j] > a[lo]).
・Exchange a[i] with a[j].
When pointers cross.
・Exchange a[lo] with a[j].
lo
K C A I E E L P U T M Q R X O S
ij
pointers cross: exchange a[lo] with a[j]
![Page 23: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/23.jpg)
Quicksort partitioning demo
Repeat until i and j pointers cross.
・Scan i from left to right so long as (a[i] < a[lo]).
・Scan j from right to left so long as (a[j] > a[lo]).
・Exchange a[i] with a[j].
When pointers cross.
・Exchange a[lo] with a[j].
lo
E C A I E K L P U T M Q R X O S
hij
partitioned!
![Page 24: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/24.jpg)
http://algs4.cs.princeton.edu
ROBERT SEDGEWICK | KEVIN WAYNE
Algorithms
2.3 PARTITIONING DEMOS
‣ Sedgewick 2-way partitioning
‣ Dijkstra 3-way partitioning
‣ Bentley-McIlroy 3-way partitioning
‣ dual-pivot partitioning
![Page 25: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/25.jpg)
・Let v be partitioning item a[lo].
・Scan i from left to right.
– (a[i] < v): exchange a[lt] with a[i]; increment both lt and i
– (a[i] > v): exchange a[gt] with a[i]; decrement gt
– (a[i] == v): increment i
Dijkstra 3-way partitioning demo
lo
P A B X W P P V P D P C Y Z
hi
lt gti
![Page 26: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/26.jpg)
・Let v be partitioning item a[lo].
・Scan i from left to right.
– (a[i] < v): exchange a[lt] with a[i]; increment both lt and i
– (a[i] > v): exchange a[gt] with a[i]; decrement gt
– (a[i] == v): increment i
Dijkstra 3-way partitioning demo
P A B X W P P V P D P C Y Z
lt gti
unknownequal
![Page 27: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/27.jpg)
・Let v be partitioning item a[lo].
・Scan i from left to right.
– (a[i] < v): exchange a[lt] with a[i]; increment both lt and i
– (a[i] > v): exchange a[gt] with a[i]; decrement gt
– (a[i] == v): increment i
Dijkstra 3-way partitioning demo
A P B X W P P V P D P C Y Z
lt gti
unknownless equal
![Page 28: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/28.jpg)
・Let v be partitioning item a[lo].
・Scan i from left to right.
– (a[i] < v): exchange a[lt] with a[i]; increment both lt and i
– (a[i] > v): exchange a[gt] with a[i]; decrement gt
– (a[i] == v): increment i
Dijkstra 3-way partitioning demo
A B P X W P P V P D P C Y Z
lt gti
unknownlessequal
![Page 29: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/29.jpg)
・Let v be partitioning item a[lo].
・Scan i from left to right.
– (a[i] < v): exchange a[lt] with a[i]; increment both lt and i
– (a[i] > v): exchange a[gt] with a[i]; decrement gt
– (a[i] == v): increment i
Dijkstra 3-way partitioning demo
A B P Z W P P V P D P C Y X
lt gti
greaterunknownless
equal
![Page 30: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/30.jpg)
・Let v be partitioning item a[lo].
・Scan i from left to right.
– (a[i] < v): exchange a[lt] with a[i]; increment both lt and i
– (a[i] > v): exchange a[gt] with a[i]; decrement gt
– (a[i] == v): increment i
Dijkstra 3-way partitioning demo
A B P Y W P P V P D P C Z X
lt gti
greaterunknownlessequal
![Page 31: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/31.jpg)
・Let v be partitioning item a[lo].
・Scan i from left to right.
– (a[i] < v): exchange a[lt] with a[i]; increment both lt and i
– (a[i] > v): exchange a[gt] with a[i]; decrement gt
– (a[i] == v): increment i
Dijkstra 3-way partitioning demo
A B P C W P P V P D P Y Z X
lt gti
less greaterunknownequal
![Page 32: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/32.jpg)
・Let v be partitioning item a[lo].
・Scan i from left to right.
– (a[i] < v): exchange a[lt] with a[i]; increment both lt and i
– (a[i] > v): exchange a[gt] with a[i]; decrement gt
– (a[i] == v): increment i
Dijkstra 3-way partitioning demo
A B C P W P P V P D P Y Z X
lt gti
less greaterunknownequal
![Page 33: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/33.jpg)
・Let v be partitioning item a[lo].
・Scan i from left to right.
– (a[i] < v): exchange a[lt] with a[i]; increment both lt and i
– (a[i] > v): exchange a[gt] with a[i]; decrement gt
– (a[i] == v): increment i
Dijkstra 3-way partitioning demo
A B C P P P P V P D W Y Z X
lt gti
less greaterunknownequal
![Page 34: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/34.jpg)
・Let v be partitioning item a[lo].
・Scan i from left to right.
– (a[i] < v): exchange a[lt] with a[i]; increment both lt and i
– (a[i] > v): exchange a[gt] with a[i]; decrement gt
– (a[i] == v): increment i
Dijkstra 3-way partitioning demo
A B C P P P P V P D W Y Z X
lt gti
less equal greaterunknown
![Page 35: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/35.jpg)
・Let v be partitioning item a[lo].
・Scan i from left to right.
– (a[i] < v): exchange a[lt] with a[i]; increment both lt and i
– (a[i] > v): exchange a[gt] with a[i]; decrement gt
– (a[i] == v): increment i
Dijkstra 3-way partitioning demo
A B C P P P P V P D W Y Z X
lt gti
less equal greaterunknown
![Page 36: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/36.jpg)
・Let v be partitioning item a[lo].
・Scan i from left to right.
– (a[i] < v): exchange a[lt] with a[i]; increment both lt and i
– (a[i] > v): exchange a[gt] with a[i]; decrement gt
– (a[i] == v): increment i
Dijkstra 3-way partitioning demo
A B C P P P P V P D W Y Z X
lt gti
less equal greaterunknown
![Page 37: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/37.jpg)
・Let v be partitioning item a[lo].
・Scan i from left to right.
– (a[i] < v): exchange a[lt] with a[i]; increment both lt and i
– (a[i] > v): exchange a[gt] with a[i]; decrement gt
– (a[i] == v): increment i
Dijkstra 3-way partitioning demo
A B C P P P P D P V W Y Z X
lt gti
less equal greaterunknown
![Page 38: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/38.jpg)
・Let v be partitioning item a[lo].
・Scan i from left to right.
– (a[i] < v): exchange a[lt] with a[i]; increment both lt and i
– (a[i] > v): exchange a[gt] with a[i]; decrement gt
– (a[i] == v): increment i
Dijkstra 3-way partitioning demo
A B C D P P P P P V W Y Z X
lt gti
less equal greaterunknown
![Page 39: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/39.jpg)
・Let v be partitioning item a[lo].
・Scan i from left to right.
– (a[i] < v): exchange a[lt] with a[i]; increment both lt and i
– (a[i] > v): exchange a[gt] with a[i]; decrement gt
– (a[i] == v): increment i
Dijkstra 3-way partitioning demo
A B C D P P P P P V W Y Z X
lt gt i
equalless greater
![Page 40: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/40.jpg)
・Let v be partitioning item a[lo].
・Scan i from left to right.
– (a[i] < v): exchange a[lt] with a[i]; increment both lt and i
– (a[i] > v): exchange a[gt] with a[i]; decrement gt
– (a[i] == v): increment i
Dijkstra 3-way partitioning demo
lo
A B C D P P P P P V W Y Z X
hilt gt
equalless greater
![Page 41: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/41.jpg)
http://algs4.cs.princeton.edu
ROBERT SEDGEWICK | KEVIN WAYNE
Algorithms
2.3 PARTITIONING DEMOS
‣ Sedgewick 2-way partitioning
‣ Dijkstra 3-way partitioning
‣ Bentley-McIlroy 3-way partitioning
‣ dual-pivot partitioning
![Page 42: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/42.jpg)
Bentley-McIlroy 3-way partitioning demo
Phase I. Repeat until i and j pointers cross.
・Scan i from left to right so long as (a[i] < a[lo]).
・Scan j from right to left so long as (a[j] > a[lo]).
・Exchange a[i] with a[j].
・If (a[i] == a[lo]), exchange a[i] with a[p] and increment p.
・If (a[j] == a[lo]), exchange a[j] with a[q] and decrement q.
lo
P A B X W P P V P D P C Y Z
i j
p q
![Page 43: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/43.jpg)
Bentley-McIlroy 3-way partitioning demo
Phase I. Repeat until i and j pointers cross.
・Scan i from left to right so long as (a[i] < a[lo]).
・Scan j from right to left so long as (a[j] > a[lo]).
・Exchange a[i] with a[j].
・If (a[i] == a[lo]), exchange a[i] with a[p] and increment p.
・If (a[j] == a[lo]), exchange a[j] with a[q] and decrement q.
lo
P A B X W P P V P D P C Y Z
i j
p q
![Page 44: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/44.jpg)
Bentley-McIlroy 3-way partitioning demo
Phase I. Repeat until i and j pointers cross.
・Scan i from left to right so long as (a[i] < a[lo]).
・Scan j from right to left so long as (a[j] > a[lo]).
・Exchange a[i] with a[j].
・If (a[i] == a[lo]), exchange a[i] with a[p] and increment p.
・If (a[j] == a[lo]), exchange a[j] with a[q] and decrement q.
lo
P A B X W P P V P D P C Y Z
i j
p q
![Page 45: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/45.jpg)
Bentley-McIlroy 3-way partitioning demo
Phase I. Repeat until i and j pointers cross.
・Scan i from left to right so long as (a[i] < a[lo]).
・Scan j from right to left so long as (a[j] > a[lo]).
・Exchange a[i] with a[j].
・If (a[i] == a[lo]), exchange a[i] with a[p] and increment p.
・If (a[j] == a[lo]), exchange a[j] with a[q] and decrement q.
lo
P A B X W P P V P D P C Y Z
i j
p q
hi
![Page 46: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/46.jpg)
Bentley-McIlroy 3-way partitioning demo
Phase I. Repeat until i and j pointers cross.
・Scan i from left to right so long as (a[i] < a[lo]).
・Scan j from right to left so long as (a[j] > a[lo]).
・Exchange a[i] with a[j].
・If (a[i] == a[lo]), exchange a[i] with a[p] and increment p.
・If (a[j] == a[lo]), exchange a[j] with a[q] and decrement q.
lo
P A B X W P P V P D P C Y Z
i j
p q
hi
exchange a[i] with a[j]
![Page 47: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/47.jpg)
Bentley-McIlroy 3-way partitioning demo
Phase I. Repeat until i and j pointers cross.
・Scan i from left to right so long as (a[i] < a[lo]).
・Scan j from right to left so long as (a[j] > a[lo]).
・Exchange a[i] with a[j].
・If (a[i] == a[lo]), exchange a[i] with a[p] and increment p.
・If (a[j] == a[lo]), exchange a[j] with a[q] and decrement q.
lo
P A B C W P P V P D P X Y Z
i j
p q
hi
![Page 48: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/48.jpg)
Bentley-McIlroy 3-way partitioning demo
Phase I. Repeat until i and j pointers cross.
・Scan i from left to right so long as (a[i] < a[lo]).
・Scan j from right to left so long as (a[j] > a[lo]).
・Exchange a[i] with a[j].
・If (a[i] == a[lo]), exchange a[i] with a[p] and increment p.
・If (a[j] == a[lo]), exchange a[j] with a[q] and decrement q.
lo
P A B C W P P V P D P X Y Z
i j
p q
hi
![Page 49: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/49.jpg)
Bentley-McIlroy 3-way partitioning demo
Phase I. Repeat until i and j pointers cross.
・Scan i from left to right so long as (a[i] < a[lo]).
・Scan j from right to left so long as (a[j] > a[lo]).
・Exchange a[i] with a[j].
・If (a[i] == a[lo]), exchange a[i] with a[p] and increment p.
・If (a[j] == a[lo]), exchange a[j] with a[q] and decrement q.
lo
P A B C W P P V P D P X Y Z
i j
p q
hi
exchange a[i] with a[j]
![Page 50: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/50.jpg)
Bentley-McIlroy 3-way partitioning demo
Phase I. Repeat until i and j pointers cross.
・Scan i from left to right so long as (a[i] < a[lo]).
・Scan j from right to left so long as (a[j] > a[lo]).
・Exchange a[i] with a[j].
・If (a[i] == a[lo]), exchange a[i] with a[p] and increment p.
・If (a[j] == a[lo]), exchange a[j] with a[q] and decrement q.
lo
P A B C P P P V P D W X Y Z
i j
p q
hi
exchange a[i] with a[p] and increment p
![Page 51: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/51.jpg)
Bentley-McIlroy 3-way partitioning demo
Phase I. Repeat until i and j pointers cross.
・Scan i from left to right so long as (a[i] < a[lo]).
・Scan j from right to left so long as (a[j] > a[lo]).
・Exchange a[i] with a[j].
・If (a[i] == a[lo]), exchange a[i] with a[p] and increment p.
・If (a[j] == a[lo]), exchange a[j] with a[q] and decrement q.
lo
P P B C A P P V P D W X Y Z
i j
p q
hi
![Page 52: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/52.jpg)
Bentley-McIlroy 3-way partitioning demo
Phase I. Repeat until i and j pointers cross.
・Scan i from left to right so long as (a[i] < a[lo]).
・Scan j from right to left so long as (a[j] > a[lo]).
・Exchange a[i] with a[j].
・If (a[i] == a[lo]), exchange a[i] with a[p] and increment p.
・If (a[j] == a[lo]), exchange a[j] with a[q] and decrement q.
lo
P P B C A P P V P D W X Y Z
i j
p q
hi
![Page 53: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/53.jpg)
Bentley-McIlroy 3-way partitioning demo
Phase I. Repeat until i and j pointers cross.
・Scan i from left to right so long as (a[i] < a[lo]).
・Scan j from right to left so long as (a[j] > a[lo]).
・Exchange a[i] with a[j].
・If (a[i] == a[lo]), exchange a[i] with a[p] and increment p.
・If (a[j] == a[lo]), exchange a[j] with a[q] and decrement q.
lo
P P B C A P P V P D W X Y Z
i j
p q
hi
exchange a[i] with a[j]
![Page 54: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/54.jpg)
Bentley-McIlroy 3-way partitioning demo
Phase I. Repeat until i and j pointers cross.
・Scan i from left to right so long as (a[i] < a[lo]).
・Scan j from right to left so long as (a[j] > a[lo]).
・Exchange a[i] with a[j].
・If (a[i] == a[lo]), exchange a[i] with a[p] and increment p.
・If (a[j] == a[lo]), exchange a[j] with a[q] and decrement q.
lo
P P B C A D P V P P W X Y Z
i j
p q
hi
exchange a[j] with a[q] and decrement q
![Page 55: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/55.jpg)
Bentley-McIlroy 3-way partitioning demo
Phase I. Repeat until i and j pointers cross.
・Scan i from left to right so long as (a[i] < a[lo]).
・Scan j from right to left so long as (a[j] > a[lo]).
・Exchange a[i] with a[j].
・If (a[i] == a[lo]), exchange a[i] with a[p] and increment p.
・If (a[j] == a[lo]), exchange a[j] with a[q] and decrement q.
lo
P P B C A D P V P Z W X Y P
i j
p q
hi
![Page 56: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/56.jpg)
Bentley-McIlroy 3-way partitioning demo
Phase I. Repeat until i and j pointers cross.
・Scan i from left to right so long as (a[i] < a[lo]).
・Scan j from right to left so long as (a[j] > a[lo]).
・Exchange a[i] with a[j].
・If (a[i] == a[lo]), exchange a[i] with a[p] and increment p.
・If (a[j] == a[lo]), exchange a[j] with a[q] and decrement q.
lo
P P B C A D P V P Z W X Y P
i j
p q
hi
![Page 57: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/57.jpg)
Bentley-McIlroy 3-way partitioning demo
Phase I. Repeat until i and j pointers cross.
・Scan i from left to right so long as (a[i] < a[lo]).
・Scan j from right to left so long as (a[j] > a[lo]).
・Exchange a[i] with a[j].
・If (a[i] == a[lo]), exchange a[i] with a[p] and increment p.
・If (a[j] == a[lo]), exchange a[j] with a[q] and decrement q.
lo
P P B C A D P V P Z W X Y P
i j
p q
hi
exchange a[i] with a[j]
![Page 58: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/58.jpg)
Bentley-McIlroy 3-way partitioning demo
Phase I. Repeat until i and j pointers cross.
・Scan i from left to right so long as (a[i] < a[lo]).
・Scan j from right to left so long as (a[j] > a[lo]).
・Exchange a[i] with a[j].
・If (a[i] == a[lo]), exchange a[i] with a[p] and increment p.
・If (a[j] == a[lo]), exchange a[j] with a[q] and decrement q.
lo
P P B C A D P V P Z W X Y P
i j
p q
hi
exchange a[i] with a[p] and increment p
![Page 59: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/59.jpg)
Bentley-McIlroy 3-way partitioning demo
Phase I. Repeat until i and j pointers cross.
・Scan i from left to right so long as (a[i] < a[lo]).
・Scan j from right to left so long as (a[j] > a[lo]).
・Exchange a[i] with a[j].
・If (a[i] == a[lo]), exchange a[i] with a[p] and increment p.
・If (a[j] == a[lo]), exchange a[j] with a[q] and decrement q.
lo
P P P C A D B V P Z W X Y P
i j
p q
hi
exchange a[j] with a[q] and decrement q
![Page 60: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/60.jpg)
Bentley-McIlroy 3-way partitioning demo
Phase I. Repeat until i and j pointers cross.
・Scan i from left to right so long as (a[i] < a[lo]).
・Scan j from right to left so long as (a[j] > a[lo]).
・Exchange a[i] with a[j].
・If (a[i] == a[lo]), exchange a[i] with a[p] and increment p.
・If (a[j] == a[lo]), exchange a[j] with a[q] and decrement q.
lo
P P P C A D B V Y Z W X P P
i j
p q
hi
![Page 61: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/61.jpg)
Bentley-McIlroy 3-way partitioning demo
Phase I. Repeat until i and j pointers cross.
・Scan i from left to right so long as (a[i] < a[lo]).
・Scan j from right to left so long as (a[j] > a[lo]).
・Exchange a[i] with a[j].
・If (a[i] == a[lo]), exchange a[i] with a[p] and increment p.
・If (a[j] == a[lo]), exchange a[j] with a[q] and decrement q.
lo
P P P C A D B V Y Z W X P P
i j
p q
hi
![Page 62: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/62.jpg)
Bentley-McIlroy 3-way partitioning demo
Phase I. Repeat until i and j pointers cross.
・Scan i from left to right so long as (a[i] < a[lo]).
・Scan j from right to left so long as (a[j] > a[lo]).
・Exchange a[i] with a[j].
・If (a[i] == a[lo]), exchange a[i] with a[p] and increment p.
・If (a[j] == a[lo]), exchange a[j] with a[q] and decrement q.
lo
P P P C A D B V Y Z W X P P
i j
p q
hi
![Page 63: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/63.jpg)
Bentley-McIlroy 3-way partitioning demo
Phase I. Repeat until i and j pointers cross.
・Scan i from left to right so long as (a[i] < a[lo]).
・Scan j from right to left so long as (a[j] > a[lo]).
・Exchange a[i] with a[j].
・If (a[i] == a[lo]), exchange a[i] with a[p] and increment p.
・If (a[j] == a[lo]), exchange a[j] with a[q] and decrement q.
lo
P P P C A D B V Y Z W X P P
p q
hiij
pointers cross
![Page 64: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/64.jpg)
Bentley-McIlroy 3-way partitioning demo
Phase II. Swap equal keys to the center.
・Scan j and p from right to left and exchange a[j] with a[p].
・Scan i and q from left to right and exchange a[i] with a[q].
lo
P P P C A D B V Y Z W X P P
ij
p q
hi
exchange a[j] with a[p]
![Page 65: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/65.jpg)
Bentley-McIlroy 3-way partitioning demo
Phase II. Swap equal keys to the center.
・Scan j and p from right to left and exchange a[j] with a[p].
・Scan i and q from left to right and exchange a[i] with a[q].
lo
P P B C A D P V Y Z W X P P
ij
p q
hi
exchange a[j] with a[p]
![Page 66: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/66.jpg)
Bentley-McIlroy 3-way partitioning demo
Phase II. Swap equal keys to the center.
・Scan j and p from right to left and exchange a[j] with a[p].
・Scan i and q from left to right and exchange a[i] with a[q].
lo
P D B C A P P V Y Z W X P P
ij
p q
hi
exchange a[j] with a[p]
![Page 67: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/67.jpg)
Bentley-McIlroy 3-way partitioning demo
Phase II. Swap equal keys to the center.
・Scan j and p from right to left and exchange a[j] with a[p].
・Scan i and q from left to right and exchange a[i] with a[q].
lo
A D B C P P P V Y Z W X P P
ij
q
hi
exchange a[i] with a[q]
![Page 68: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/68.jpg)
Bentley-McIlroy 3-way partitioning demo
Phase II. Swap equal keys to the center.
・Scan j and p from right to left and exchange a[j] with a[p].
・Scan i and q from left to right and exchange a[i] with a[q].
lo
A D B C P P P P Y Z W X V P
ij
q
hi
exchange a[i] with a[q]
![Page 69: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/69.jpg)
Bentley-McIlroy 3-way partitioning demo
Phase II. Swap equal keys to the center.
・Scan j and p from right to left and exchange a[j] with a[p].
・Scan i and q from left to right and exchange a[i] with a[q].
lo
A D B C P P P P P Z W X V Y
ij hi
3-way partitioned
![Page 70: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/70.jpg)
http://algs4.cs.princeton.edu
ROBERT SEDGEWICK | KEVIN WAYNE
Algorithms
2.3 PARTITIONING DEMOS
‣ Sedgewick 2-way partitioning
‣ Dijkstra 3-way partitioning
‣ Bentley-McIlroy 3-way partitioning
‣ dual-pivot partitioning
![Page 71: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/71.jpg)
Dual-pivot partitioning demo
Initialization.
・Choose a[lo] and a[hi] as partitioning items.
・Exchange if necessary to ensure a[lo] ≤ a[hi].
S E A Y R L F V Z Q T C M K
lo hi
exchange a[lo] and a[hi]
![Page 72: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/72.jpg)
Dual-pivot partitioning demo
Initialization.
・Choose a[lo] and a[hi] as partitioning items.
・Exchange if necessary to ensure a[lo] ≤ a[hi].
K E A Y R L F V Z Q T C M S
lo hi
![Page 73: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/73.jpg)
Dual-pivot partitioning demo
Main loop. Repeat until i and gt pointers cross.
・If (a[i] < a[lo]), exchange a[i] with a[lt] and increment lt and i.
・Else if (a[i] > a[hi]), exchange a[i] with a[gt] and decrement gt.
・Else, increment i.
K E A Y R L F V Z Q T C M S
p1 < p1 p1 ≤ and ≤ p2 ? > p2 p2
lo i hilt gt
gtlt ilo hi
exchange a[i] and a[lt]; increment lt and i
![Page 74: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/74.jpg)
Dual-pivot partitioning demo
Main loop. Repeat until i and gt pointers cross.
・If (a[i] < a[lo]), exchange a[i] with a[lt] and increment lt and i.
・Else if (a[i] > a[hi]), exchange a[i] with a[gt] and decrement gt.
・Else, increment i.
K E A Y R L F V Z Q T C M S
p1 < p1 p1 ≤ and ≤ p2 ? > p2 p2
lo i hilt gt
gtlt ilo hi
exchange a[i] and a[lt]; increment lt and i
![Page 75: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/75.jpg)
Dual-pivot partitioning demo
Main loop. Repeat until i and gt pointers cross.
・If (a[i] < a[lo]), exchange a[i] with a[lt] and increment lt and i.
・Else if (a[i] > a[hi]), exchange a[i] with a[gt] and decrement gt.
・Else, increment i.
K E A Y R L F V Z Q T C M S
p1 < p1 p1 ≤ and ≤ p2 ? > p2 p2
lo i hilt gt
gtlt ilo hi
exchange a[i] and a[gt]; decrement gt
![Page 76: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/76.jpg)
Dual-pivot partitioning demo
Main loop. Repeat until i and gt pointers cross.
・If (a[i] < a[lo]), exchange a[i] with a[lt] and increment lt and i.
・Else if (a[i] > a[hi]), exchange a[i] with a[gt] and decrement gt.
・Else, increment i.
K E A M R L F V Z Q T C Y S
p1 < p1 p1 ≤ and ≤ p2 ? > p2 p2
lo i hilt gt
gtlt ilo hi
increment i
![Page 77: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/77.jpg)
Dual-pivot partitioning demo
Main loop. Repeat until i and gt pointers cross.
・If (a[i] < a[lo]), exchange a[i] with a[lt] and increment lt and i.
・Else if (a[i] > a[hi]), exchange a[i] with a[gt] and decrement gt.
・Else, increment i.
K E A M R L F V Z Q T C Y S
p1 < p1 p1 ≤ and ≤ p2 ? > p2 p2
lo i hilt gt
gtlt ilo hi
increment i
![Page 78: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/78.jpg)
Dual-pivot partitioning demo
Main loop. Repeat until i and gt pointers cross.
・If (a[i] < a[lo]), exchange a[i] with a[lt] and increment lt and i.
・Else if (a[i] > a[hi]), exchange a[i] with a[gt] and decrement gt.
・Else, increment i.
K E A M R L F V Z Q T C Y S
p1 < p1 p1 ≤ and ≤ p2 ? > p2 p2
lo i hilt gt
gtlt ilo hi
increment i
![Page 79: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/79.jpg)
Dual-pivot partitioning demo
Main loop. Repeat until i and gt pointers cross.
・If (a[i] < a[lo]), exchange a[i] with a[lt] and increment lt and i.
・Else if (a[i] > a[hi]), exchange a[i] with a[gt] and decrement gt.
・Else, increment i.
K E A M R L F V Z Q T C Y S
p1 < p1 p1 ≤ and ≤ p2 ? > p2 p2
lo i hilt gt
gtlt ilo hi
exchange a[i] and a[lt]; increment lt and i
![Page 80: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/80.jpg)
Dual-pivot partitioning demo
Main loop. Repeat until i and gt pointers cross.
・If (a[i] < a[lo]), exchange a[i] with a[lt] and increment lt and i.
・Else if (a[i] > a[hi]), exchange a[i] with a[gt] and decrement gt.
・Else, increment i.
K E A F R L M V Z Q T C Y S
p1 < p1 p1 ≤ and ≤ p2 ? > p2 p2
lo i hilt gt
gtlt ilo hi
exchange a[i] and a[gt]; decrement gt
![Page 81: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/81.jpg)
Dual-pivot partitioning demo
Main loop. Repeat until i and gt pointers cross.
・If (a[i] < a[lo]), exchange a[i] with a[lt] and increment lt and i.
・Else if (a[i] > a[hi]), exchange a[i] with a[gt] and decrement gt.
・Else, increment i.
K E A F R L M C Z Q T V Y S
p1 < p1 p1 ≤ and ≤ p2 ? > p2 p2
lo i hilt gt
gtlt ilo hi
exchange a[i] and a[lt]; increment lt and i
![Page 82: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/82.jpg)
Dual-pivot partitioning demo
Main loop. Repeat until i and gt pointers cross.
・If (a[i] < a[lo]), exchange a[i] with a[lt] and increment lt and i.
・Else if (a[i] > a[hi]), exchange a[i] with a[gt] and decrement gt.
・Else, increment i.
K E A F C L M R Z Q T V Y S
p1 < p1 p1 ≤ and ≤ p2 ? > p2 p2
lo i hilt gt
gtlt ilo hi
exchange a[i] and a[gt]; decrement gt
![Page 83: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/83.jpg)
Dual-pivot partitioning demo
Main loop. Repeat until i and gt pointers cross.
・If (a[i] < a[lo]), exchange a[i] with a[lt] and increment lt and i.
・Else if (a[i] > a[hi]), exchange a[i] with a[gt] and decrement gt.
・Else, increment i.
K E A F C L M R T Q Z V Y S
p1 < p1 p1 ≤ and ≤ p2 ? > p2 p2
lo i hilt gt
gtlt ilo hi
exchange a[i] and a[gt]; decrement gt
![Page 84: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/84.jpg)
Dual-pivot partitioning demo
Main loop. Repeat until i and gt pointers cross.
・If (a[i] < a[lo]), exchange a[i] with a[lt] and increment lt and i.
・Else if (a[i] > a[hi]), exchange a[i] with a[gt] and decrement gt.
・Else, increment i.
K E A F C L M R Q T Z V Y S
gtlt ilo hi
increment i
p1 < p1 p1 ≤ and ≤ p2 ? > p2 p2
lo i hilt gt
![Page 85: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/85.jpg)
Dual-pivot partitioning demo
Main loop. Repeat until i and gt pointers cross.
・If (a[i] < a[lo]), exchange a[i] with a[lt] and increment lt and i.
・Else if (a[i] > a[hi]), exchange a[i] with a[gt] and decrement gt.
・Else, increment i.
K E A F C L M R Q T Z V Y S
p1 < p1 p1 ≤ and ≤ p2 ? > p2 p2
lo i hilt gt
gtlt ilo hi
stop when pointers cross
![Page 86: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/86.jpg)
Dual-pivot partitioning demo
Finalize.
・Exchange a[lo] with a[--lt].
・Exchange a[hi] with a[++gt].
K E A F C L M R Q T Z V Y S
p1 < p1 p1 ≤ and ≤ p2 > p2 p2
lo hilt gt
gtltlo hi
![Page 87: Algorithmsyoshi/Sedgewick/Algs4th/Slides/...lt i gt less unknown equal ・Let v be partitioning item a[lo]. ・Scan i from left to right. – (a[i] < v): exchange a[lt] with a[i];](https://reader036.vdocument.in/reader036/viewer/2022081620/611a2779dc7654661279f49a/html5/thumbnails/87.jpg)
Dual-pivot partitioning demo
Finalize.
・Exchange a[lo] with a[--lt].
・Exchange a[hi] with a[++gt].
C E A F K L M R Q S Z V Y T
gtltlo hi
3-way partitioned
< p1 p1 p1 ≤ and ≤ p2 p2 > p2
lo hilt gt