QUICKSORT PARTITIONING EMO€¦ · 3 Quicksort partitioning Repeat until i and j pointers cross....

68
Algorithms, 4 th Edition · Robert Sedgewick and Kevin Wayne · Copyright © 2002–2011 · September 24, 2011 8:19:02 AM Algorithms F O U R T H E D I T I O N R O B E R T S E D G E W I C K K E V I N W AY N E 2.3 QUICKSORT PARTITIONING DEMO Sedgewick 2-way Dijkstra 3-way Bentley-McIlroy 3-way

Transcript of QUICKSORT PARTITIONING EMO€¦ · 3 Quicksort partitioning Repeat until i and j pointers cross....

  • Algorithms, 4th Edition · Robert Sedgewick and Kevin Wayne · Copyright © 2002–2011 · September 24, 2011 8:19:02 AM

    AlgorithmsF O U R T H E D I T I O N

    R O B E R T S E D G E W I C K K E V I N W A Y N E

    2.3 QUICKSORT PARTITIONING DEMO

    ‣ Sedgewick 2-way‣ Dijkstra 3-way‣ Bentley-McIlroy 3-way

  • Algorithms, 4th Edition · Robert Sedgewick and Kevin Wayne · Copyright © 2002–2011 · September 24, 2011 8:19:02 AM

    AlgorithmsF O U R T H E D I T I O N

    R O B E R T S E D G E W I C K K E V I N W A Y N E

    SEDGEWICK 2-WAY PARTITIONING

  • 3

    Quicksort partitioning

    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]

  • 4

    Quicksort partitioning

    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

  • 5

    Quicksort partitioning

    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

  • 6

    Quicksort partitioning

    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]

  • 7

    Quicksort partitioning

    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

  • 8

    Quicksort partitioning

    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

  • 9

    Quicksort partitioning

    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]

  • 10

    Quicksort partitioning

    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

  • 11

    Quicksort partitioning

    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

  • 12

    Quicksort partitioning

    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]

  • 13

    Quicksort partitioning

    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

  • 14

    Quicksort partitioning

    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

  • 15

    Quicksort partitioning

    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]

  • 16

    Quicksort partitioning

    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

  • 17

    Quicksort partitioning

    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

  • 18

    Quicksort partitioning

    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]

  • 19

    Quicksort partitioning

    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

  • 20

    Quicksort partitioning

    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

    stop i scan because a[i] >= a[lo]

    j

  • 21

    Quicksort partitioning

    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]

  • 22

    Quicksort partitioning

    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]

  • 23

    Quicksort partitioning

    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!

  • Algorithms, 4th Edition · Robert Sedgewick and Kevin Wayne · Copyright © 2002–2011 · September 24, 2011 8:19:02 AM

    AlgorithmsF O U R T H E D I T I O N

    R O B E R T S E D G E W I C K K E V I N W A Y N E

    DIJKSTRA 3-WAY PARTITIONING

  • • Let v be partitioning item a[lo].• Scan i from left to right.- (a[i] < v): exchange a[lt] with a[i] and increment both lt and i- (a[i] > v): exchange a[gt] with a[i] and decrement gt- (a[i] == v): increment i

    25

    Dijkstra 3-way partitioning

    lo

    P A B X W P P V P D P C Y Z

    hi

    lt gt

    lt

    v

    gti

    v

    >v

  • • Let v be partitioning item a[lo].• Scan i from left to right.- (a[i] < v): exchange a[lt] with a[i] and increment both lt and i- (a[i] > v): exchange a[gt] with a[i] and decrement gt- (a[i] == v): increment i

    26

    Dijkstra 3-way partitioning

    P A B X W P P V P D P C Y Z

    lt gt

    lt

    v

    gti

    v

    >v

  • • Let v be partitioning item a[lo].• Scan i from left to right.- (a[i] < v): exchange a[lt] with a[i] and increment both lt and i- (a[i] > v): exchange a[gt] with a[i] and decrement gt- (a[i] == v): increment i

    27

    Dijkstra 3-way partitioning

    A P B X W P P V P D P C Y Z

    lt gt

    lt

    v

    gti

    v

    >v

  • • Let v be partitioning item a[lo].• Scan i from left to right.- (a[i] < v): exchange a[lt] with a[i] and increment both lt and i- (a[i] > v): exchange a[gt] with a[i] and decrement gt- (a[i] == v): increment i

    28

    Dijkstra 3-way partitioning

    A B P X W P P V P D P C Y Z

    lt gt

    lt

    v

    gti

    v

    >v

  • • Let v be partitioning item a[lo].• Scan i from left to right.- (a[i] < v): exchange a[lt] with a[i] and increment both lt and i- (a[i] > v): exchange a[gt] with a[i] and decrement gt- (a[i] == v): increment i

    29

    Dijkstra 3-way partitioning

    A B P Z W P P V P D P C Y X

    lt gt

    lt

    v

    gti

    v

    >v

  • • Let v be partitioning item a[lo].• Scan i from left to right.- (a[i] < v): exchange a[lt] with a[i] and increment both lt and i- (a[i] > v): exchange a[gt] with a[i] and decrement gt- (a[i] == v): increment i

    30

    Dijkstra 3-way partitioning

    A B P Y W P P V P D P C Z X

    lt gt

    lt

    v

    gti

    v

    >v

  • • Let v be partitioning item a[lo].• Scan i from left to right.- (a[i] < v): exchange a[lt] with a[i] and increment both lt and i- (a[i] > v): exchange a[gt] with a[i] and decrement gt- (a[i] == v): increment i

    31

    Dijkstra 3-way partitioning

    A B P C W P P V P D P Y Z X

    lt gt

    lt

    v

    gti

    v

    >v

  • • Let v be partitioning item a[lo].• Scan i from left to right.- (a[i] < v): exchange a[lt] with a[i] and increment both lt and i- (a[i] > v): exchange a[gt] with a[i] and decrement gt- (a[i] == v): increment i

    32

    Dijkstra 3-way partitioning

    A B C P W P P V P D P Y Z X

    lt gt

    lt

    v

    gti

    v

    >v

  • • Let v be partitioning item a[lo].• Scan i from left to right.- (a[i] < v): exchange a[lt] with a[i] and increment both lt and i- (a[i] > v): exchange a[gt] with a[i] and decrement gt- (a[i] == v): increment i

    33

    Dijkstra 3-way partitioning

    A B C P P P P V P D W Y Z X

    lt gt

    lt

    v

    gti

    v

    >v

  • • Let v be partitioning item a[lo].• Scan i from left to right.- (a[i] < v): exchange a[lt] with a[i] and increment both lt and i- (a[i] > v): exchange a[gt] with a[i] and decrement gt- (a[i] == v): increment i

    34

    Dijkstra 3-way partitioning

    A B C P P P P V P D W Y Z X

    lt gt

    lt

    v

    gti

    v

    >v

  • • Let v be partitioning item a[lo].• Scan i from left to right.- (a[i] < v): exchange a[lt] with a[i] and increment both lt and i- (a[i] > v): exchange a[gt] with a[i] and decrement gt- (a[i] == v): increment i

    35

    Dijkstra 3-way partitioning

    A B C P P P P V P D W Y Z X

    lt gt

    lt

    v

    gti

    v

    >v

  • • Let v be partitioning item a[lo].• Scan i from left to right.- (a[i] < v): exchange a[lt] with a[i] and increment both lt and i- (a[i] > v): exchange a[gt] with a[i] and decrement gt- (a[i] == v): increment i

    36

    Dijkstra 3-way partitioning

    A B C P P P P V P D W Y Z X

    lt gt

    lt

    v

    gti

    v

    >v

  • • Let v be partitioning item a[lo].• Scan i from left to right.- (a[i] < v): exchange a[lt] with a[i] and increment both lt and i- (a[i] > v): exchange a[gt] with a[i] and decrement gt- (a[i] == v): increment i

    37

    Dijkstra 3-way partitioning

    A B C P P P P D P V W Y Z X

    lt gt

    lt

    v

    gti

    v

    >v

  • • Let v be partitioning item a[lo].• Scan i from left to right.- (a[i] < v): exchange a[lt] with a[i] and increment both lt and i- (a[i] > v): exchange a[gt] with a[i] and decrement gt- (a[i] == v): increment i

    38

    Dijkstra 3-way partitioning

    A B C D P P P P P V W Y Z X

    lt gt

    lt

    v

    gti

    v

    >v

  • • Let v be partitioning item a[lo].• Scan i from left to right.- (a[i] < v): exchange a[lt] with a[i] and increment both lt and i- (a[i] > v): exchange a[gt] with a[i] and decrement gt- (a[i] == v): increment i

    39

    Dijkstra 3-way partitioning

    lo

    A B C D P P P P P V W Y Z X

    hi

    lt gt

    lt

    v

    gti

    v

    >v

  • Algorithms, 4th Edition · Robert Sedgewick and Kevin Wayne · Copyright © 2002–2011 · September 24, 2011 8:19:02 AM

    AlgorithmsF O U R T H E D I T I O N

    R O B E R T S E D G E W I C K K E V I N W A Y N E

    BENTLEY-MCILROY 3-WAY PARTITIONING

  • 41

    Bentley-McIlroy 3-way partitioning

    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

  • 42

    Bentley-McIlroy 3-way partitioning

    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

  • 43

    Bentley-McIlroy 3-way partitioning

    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

  • 44

    Bentley-McIlroy 3-way partitioning

    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

  • 45

    Bentley-McIlroy 3-way partitioning

    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]

  • 46

    Bentley-McIlroy 3-way partitioning

    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

  • 47

    Bentley-McIlroy 3-way partitioning

    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

  • 48

    Bentley-McIlroy 3-way partitioning

    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]

  • 49

    Bentley-McIlroy 3-way partitioning

    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

  • 50

    Bentley-McIlroy 3-way partitioning

    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

  • 51

    Bentley-McIlroy 3-way partitioning

    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

  • 52

    Bentley-McIlroy 3-way partitioning

    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]

  • 53

    Bentley-McIlroy 3-way partitioning

    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

  • 54

    Bentley-McIlroy 3-way partitioning

    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

  • 55

    Bentley-McIlroy 3-way partitioning

    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

  • 56

    Bentley-McIlroy 3-way partitioning

    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]

  • 57

    Bentley-McIlroy 3-way partitioning

    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

  • 58

    Bentley-McIlroy 3-way partitioning

    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

  • 59

    Bentley-McIlroy 3-way partitioning

    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

  • 60

    Bentley-McIlroy 3-way partitioning

    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

  • 61

    Bentley-McIlroy 3-way partitioning

    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

  • 62

    Bentley-McIlroy 3-way partitioning

    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

  • 63

    Bentley-McIlroy 3-way partitioning

    Afterwards, 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]

  • 64

    Bentley-McIlroy 3-way partitioning

    Afterwards, 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]

  • 65

    Bentley-McIlroy 3-way partitioning

    Afterwards, 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]

  • 66

    Bentley-McIlroy 3-way partitioning

    Afterwards, 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]

  • 67

    Bentley-McIlroy 3-way partitioning

    Afterwards, 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]

  • 68

    Bentley-McIlroy 3-way partitioning

    Afterwards, 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