algorithmsyoshi/sedgewick/algs4th/slides/...lt i gt less unknown equal ・let v be partitioning item...

87
ROBERT S EDGEWICK | KEVIN WAYNE FOURTH EDITION Algorithms http://algs4.cs.princeton.edu Algorithms R OBERT S EDGEWICK | K EVIN W AYNE 2.3 P ARTITIONING D EMOS Sedgewick 2-way partitioning Dijkstra 3-way partitioning Bentley-McIlroy 3-way partitioning dual-pivot partitioning

Upload: others

Post on 18-Mar-2021

2 views

Category:

Documents


0 download

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

・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];

・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];

・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];

・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];

・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];

・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];

・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];

・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];

・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];

・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];

・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];

・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];

・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];

・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];

・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];

・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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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];

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