Quicksort Partitioning€¦ · 2 Quicksort partitioning Repeat until i and j pointers cross. •...

67
Algorithms, 4 th Edition · Robert Sedgewick and Kevin Wayne · Copyright © 2002–2010 · October 2, 2010 8:58:45 AM Quicksort Partitioning a[i] i j 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 16 K R A T E L E P U I M Q C X O S 1 12 K R A T E L E P U I M Q C X O S 1 12 K C A T E L E P U I M Q R X O S 3 9 K C A T E L E P U I M Q R X O S 3 9 K C A I E L E P U T M Q R X O S 5 6 K C A I E L E P U T M Q R X O S 5 6 K C A I E E L P U T M Q R X O S 6 5 K C A I E E L P U T M Q R X O S 6 5 E C A I E K L P U T M Q R X O S 6 5 E C A I E K L P U T M Q R X O S Partitioning trace (array contents before and after each exchange) initial values scan left, scan right exchange scan left, scan right exchange scan left, scan right exchange scan left, scan right nal exchange result v

Transcript of Quicksort Partitioning€¦ · 2 Quicksort partitioning Repeat until i and j pointers cross. •...

  • Algorithms, 4th Edition · Robert Sedgewick and Kevin Wayne · Copyright © 2002–2010 · October 2, 2010 8:58:45 AM

    Quicksort Partitioning

    a[i] i j 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

    0 16 K R A T E L E P U I M Q C X O S

    1 12 K R A T E L E P U I M Q C X O S

    1 12 K C A T E L E P U I M Q R X O S

    3 9 K C A T E L E P U I M Q R X O S

    3 9 K C A I E L E P U T M Q R X O S

    5 6 K C A I E L E P U T M Q R X O S

    5 6 K C A I E E L P U T M Q R X O S

    6 5 K C A I E E L P U T M Q R X O S

    6 5 E C A I E K L P U T M Q R X O S

    6 5 E C A I E K L P U T M Q R X O S

    Partitioning trace (array contents before and after each exchange)

    initial values

    scan left, scan right

    exchange

    scan left, scan right

    exchange

    scan left, scan right

    exchange

    scan left, scan right

    !nal exchange

    result

    v

  • 2

    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]

  • 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

  • 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

    stop j scan and exchange a[i] with a[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 C A T E L E P U I M Q R X O S

    i 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

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

  • 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

  • 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

    stop j scan and exchange a[i] with a[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 I E L E P U T M Q R X O S

    i 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

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

  • 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

  • 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

    stop j scan and exchange a[i] with a[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 E L P U T M Q R X O S

    i 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

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

    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

    ij

    stop j scan because a[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].

    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]

  • 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

    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–2010 · October 2, 2010 8:58:45 AM

    Dijkstra 3-Way Partitioning

  • • Let v be partitioning element 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

    24

    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 element 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

    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 element 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

    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 element 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 B P X W P P V P D P C Y Z

    lt gt

    lt

    v

    gti

    v

    >v

  • • Let v be partitioning element 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 Z W P P V P D P C Y X

    lt gt

    lt

    v

    gti

    v

    >v

  • • Let v be partitioning element 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 Y W P P V P D P C Z X

    lt gt

    lt

    v

    gti

    v

    >v

  • • Let v be partitioning element 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 C W P P V P D P Y Z X

    lt gt

    lt

    v

    gti

    v

    >v

  • • Let v be partitioning element 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 C P W P P V P D P Y Z X

    lt gt

    lt

    v

    gti

    v

    >v

  • • Let v be partitioning element 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 P P P V P D W Y Z X

    lt gt

    lt

    v

    gti

    v

    >v

  • • Let v be partitioning element 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 element 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 element 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 element 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 D P V W Y Z X

    lt gt

    lt

    v

    gti

    v

    >v

  • • Let v be partitioning element 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 D P P P P P V W Y Z X

    lt gt

    lt

    v

    gti

    v

    >v

  • • Let v be partitioning element 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

    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–2010 · October 2, 2010 8:58:45 AM

    Bentley-McIlroy 3-Way Partitioning

  • 40

    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

  • 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

    hi

  • 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

    exchange a[i] with a[j]

  • 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 C W P P V P D P X Y Z

    i j

    p q

    hi

  • 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

    exchange a[i] with a[j]

  • 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 P P P V P D W X Y Z

    i j

    p q

    hi

    exchange a[i] with a[p] and increment p

  • 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 P B C A P P V P D W X Y Z

    i j

    p q

    hi

  • 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

    exchange a[i] with a[j]

  • 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 D P V P P W X Y Z

    i j

    p q

    hi

    exchange a[j] with a[q] and decrement q

  • 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 Z W X Y P

    i j

    p q

    hi

  • 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

    exchange a[i] with a[j]

  • 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[p] and increment p

  • 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 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

  • 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 Y Z W X P P

    i j

    p q

    hi

  • 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

    p q

    hiij

    pointers cross

  • 62

    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]

  • 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 B C A D P 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 D B C A P 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

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

    ij

    q

    hi

    exchange a[i] with a[q]

  • 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 P Y Z W X V 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 P Z W X V Y

    ij hi

    3-way partitioned