Quicksort Partitioning Demo Sedgewick 2 way Dijkstra 3

  • Slides: 68
Download presentation
Quicksort Partitioning Demo ‣ Sedgewick 2 -way ‣ Dijkstra 3 -way ‣ Bentley-Mc. Ilroy

Quicksort Partitioning Demo ‣ Sedgewick 2 -way ‣ Dijkstra 3 -way ‣ Bentley-Mc. Ilroy 3 -way Algorithms, 4 th Edition ∙ Robert Sedgewick and Kevin Wayne ∙ Copyright © 2002– 2012 ∙ September 10, 2014 11: 22: 20 AM

Sedgewick 2 -way Partitioning Algorithms, 4 th Edition ∙ Robert Sedgewick and Kevin Wayne

Sedgewick 2 -way Partitioning Algorithms, 4 th Edition ∙ Robert Sedgewick and Kevin Wayne ∙ Copyright © 2002– 2012 ∙ September 10, 2014 11: 22: 20 AM

Quicksort partitioning Repeat until i and j pointers cross. • Scan i from left

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]. K lo R A T E L E P U I M i Q C X O S j stop i scan because a[i] >= a[lo] 3

Quicksort partitioning Repeat until i and j pointers cross. • Scan i from left

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]. K lo R i A T E L E P U I M Q C X O S j 4

Quicksort partitioning Repeat until i and j pointers cross. • Scan i from left

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]. K lo R i A T E L E P U I M Q C X O S j 5

Quicksort partitioning Repeat until i and j pointers cross. • Scan i from left

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]. K lo R A T E L E P U I M Q i C X O S 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

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]. K lo C i A T E L E P U I M Q R X O S j 7

Quicksort partitioning Repeat until i and j pointers cross. • Scan i from left

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]. K lo C A i T E L E P U I M Q R X O S j 8

Quicksort partitioning Repeat until i and j pointers cross. • Scan i from left

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]. K lo C A T E L E P U I M i Q R X O S j stop i scan because a[i] >= a[lo] 9

Quicksort partitioning Repeat until i and j pointers cross. • Scan i from left

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]. K lo C A T i E L E P U I M Q R X O S j 10

Quicksort partitioning Repeat until i and j pointers cross. • Scan i from left

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]. K lo C A T i E L E P U I M Q R X O S j 11

Quicksort partitioning Repeat until i and j pointers cross. • Scan i from left

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]. K lo C A T i E L E P U I M Q R X O S 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

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]. K lo C A I i E L E P U T M Q R X O S j 13

Quicksort partitioning Repeat until i and j pointers cross. • Scan i from left

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]. K lo C A I E i L E P U T M Q R X O S j 14

Quicksort partitioning Repeat until i and j pointers cross. • Scan i from left

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]. K lo C A I E L i E P U T M Q R X O S j stop i scan because a[i] >= a[lo] 15

Quicksort partitioning Repeat until i and j pointers cross. • Scan i from left

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]. K lo C A I E L i E P U T M Q R X O S j 16

Quicksort partitioning Repeat until i and j pointers cross. • Scan i from left

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]. K lo C A I E L i E P U T M Q R X O S j 17

Quicksort partitioning Repeat until i and j pointers cross. • Scan i from left

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

Quicksort partitioning Repeat until i and j pointers cross. • Scan i from left

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]. K lo C A I E E L i j P U T M Q R X O S 19

Quicksort partitioning Repeat until i and j pointers cross. • Scan i from left

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]. K lo 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] 20

Quicksort partitioning Repeat until i and j pointers cross. • Scan i from left

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]. K lo C A I E E L j i P U T M stop j scan because a[j] <= a[lo] Q R X O S 21

Quicksort partitioning Repeat until i and j pointers cross. • Scan i from left

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]. K lo C A I E E L j i P U T M Q pointers cross: exchange a[lo] with a[j] R X O S 22

Quicksort partitioning Repeat until i and j pointers cross. • Scan i from left

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]. E lo C A I E K L P j U T M Q R X O S hi partitioned! 23

Dijkstra 3 -Way Partitioning Algorithms, 4 th Edition ∙ Robert Sedgewick and Kevin Wayne

Dijkstra 3 -Way Partitioning Algorithms, 4 th Edition ∙ Robert Sedgewick and Kevin Wayne ∙ Copyright © 2002– 2012 ∙ September 10, 2014 11: 22: 20 AM

Dijkstra 3 -way partitioning • • Let v be partitioning item a[lo]. Scan i

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 lt i P gt A B X W lo P P V P D P C Y Z hi invariant 25

Dijkstra 3 -way partitioning • • Let v be partitioning item a[lo]. Scan i

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 lt P i A gt B X W P P V P D P C Y Z invariant 26

Dijkstra 3 -way partitioning • • Let v be partitioning item a[lo]. Scan i

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 lt A P i B gt X W P P V P D P C Y Z invariant 27

Dijkstra 3 -way partitioning • • Let v be partitioning item a[lo]. Scan i

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 lt A B P i gt X W P P V P D P C Y Z invariant 28

Dijkstra 3 -way partitioning • • Let v be partitioning item a[lo]. Scan i

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 lt A B P i gt Z W P P V P D P C Y X invariant 29

Dijkstra 3 -way partitioning • • Let v be partitioning item a[lo]. Scan i

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 lt A B P i gt Y W P P V P D P C Z X invariant 30

Dijkstra 3 -way partitioning • • Let v be partitioning item a[lo]. Scan i

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 lt A B P i gt C W P P V P D P Y Z X invariant 31

Dijkstra 3 -way partitioning • • Let v be partitioning item a[lo]. Scan i

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 lt A B C i P W gt P P V P D P Y Z X invariant 32

Dijkstra 3 -way partitioning • • Let v be partitioning item a[lo]. Scan i

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 lt A B C i P P gt P P V P D W Y Z X invariant 33

Dijkstra 3 -way partitioning • • Let v be partitioning item a[lo]. Scan i

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 lt A B C i P P P gt P V P D W Y Z X invariant 34

Dijkstra 3 -way partitioning • • Let v be partitioning item a[lo]. Scan i

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 lt A B C i P P gt V P D W Y Z X invariant 35

Dijkstra 3 -way partitioning • • Let v be partitioning item a[lo]. Scan i

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 lt A B C i P P V gt P D W Y Z X invariant 36

Dijkstra 3 -way partitioning • • Let v be partitioning item a[lo]. Scan i

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 lt A B C i P P D gt P V W Y Z X invariant 37

Dijkstra 3 -way partitioning • • Let v be partitioning item a[lo]. Scan i

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 lt A B C D P i gt P P V W Y Z X invariant 38

Dijkstra 3 -way partitioning • • Let v be partitioning item a[lo]. Scan i

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 lt A B C D P lo gt P P V W Y Z X hi invariant 39

Bentley-Mc. Ilroy 3 -Way Partitioning Algorithms, 4 th Edition ∙ Robert Sedgewick and Kevin

Bentley-Mc. Ilroy 3 -Way Partitioning Algorithms, 4 th Edition ∙ Robert Sedgewick and Kevin Wayne ∙ Copyright © 2002– 2012 ∙ September 10, 2014 11: 22: 20 AM

Bentley-Mc. Ilroy 3 -way partitioning Repeat until i and j pointers cross. • Scan

Bentley-Mc. Ilroy 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. p P lo A i q B X W P P V P D P C Y Z j 41

Bentley-Mc. Ilroy 3 -way partitioning Repeat until i and j pointers cross. • Scan

Bentley-Mc. Ilroy 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. p P lo A q B i X W P P V P D P C Y Z j 42

Bentley-Mc. Ilroy 3 -way partitioning Repeat until i and j pointers cross. • Scan

Bentley-Mc. Ilroy 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. p P lo A q B X i W P P V P D P C Y Z j 43

Bentley-Mc. Ilroy 3 -way partitioning Repeat until i and j pointers cross. • Scan

Bentley-Mc. Ilroy 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. p P lo A q B X i W P P V P D P C Y j Z hi 44

Bentley-Mc. Ilroy 3 -way partitioning Repeat until i and j pointers cross. • Scan

Bentley-Mc. Ilroy 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. p P lo A q B X W P P V P i D P C j exchange a[i] with a[j] Y Z hi 45

Bentley-Mc. Ilroy 3 -way partitioning Repeat until i and j pointers cross. • Scan

Bentley-Mc. Ilroy 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. p P lo A q B C i W P P V P D P X j Y Z hi 46

Bentley-Mc. Ilroy 3 -way partitioning Repeat until i and j pointers cross. • Scan

Bentley-Mc. Ilroy 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. p P lo A q B C W i P P V P D P X j Y Z hi 47

Bentley-Mc. Ilroy 3 -way partitioning Repeat until i and j pointers cross. • Scan

Bentley-Mc. Ilroy 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. p P lo A q B C W P P V P i D P j exchange a[i] with a[j] X Y Z hi 48

Bentley-Mc. Ilroy 3 -way partitioning Repeat until i and j pointers cross. • Scan

Bentley-Mc. Ilroy 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. p P lo A q B C P P P V P D i exchange a[i] with a[p] and increment p W j X Y Z hi 49

Bentley-Mc. Ilroy 3 -way partitioning Repeat until i and j pointers cross. • Scan

Bentley-Mc. Ilroy 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. p P lo P B q C A i P P V P D W j X Y Z hi 50

Bentley-Mc. Ilroy 3 -way partitioning Repeat until i and j pointers cross. • Scan

Bentley-Mc. Ilroy 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. p P lo P B q C A P i P V P D W j X Y Z hi 51

Bentley-Mc. Ilroy 3 -way partitioning Repeat until i and j pointers cross. • Scan

Bentley-Mc. Ilroy 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. p P lo P B q C A P P V P i exchange a[i] with a[j] D j W X Y Z hi 52

Bentley-Mc. Ilroy 3 -way partitioning Repeat until i and j pointers cross. • Scan

Bentley-Mc. Ilroy 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. p P lo P B q C A D i P V P P j exchange a[j] with a[q] and decrement q W X Y Z hi 53

Bentley-Mc. Ilroy 3 -way partitioning Repeat until i and j pointers cross. • Scan

Bentley-Mc. Ilroy 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. p P lo P B q C A D i P V P Z j W X Y P hi 54

Bentley-Mc. Ilroy 3 -way partitioning Repeat until i and j pointers cross. • Scan

Bentley-Mc. Ilroy 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. p P lo P B q C A D P i V P Z j W X Y P hi 55

Bentley-Mc. Ilroy 3 -way partitioning Repeat until i and j pointers cross. • Scan

Bentley-Mc. Ilroy 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. p P lo P B q C A D P i V P j exchange a[i] with a[j] Z W X Y P hi 56

Bentley-Mc. Ilroy 3 -way partitioning Repeat until i and j pointers cross. • Scan

Bentley-Mc. Ilroy 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. p P lo P B q C A D P i V P Z j exchange a[i] with a[p] and increment p W X Y P hi 57

Bentley-Mc. Ilroy 3 -way partitioning Repeat until i and j pointers cross. • Scan

Bentley-Mc. Ilroy 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. p P lo P P C q A D B i V P Z j exchange a[j] with a[q] and decrement q W X Y P hi 58

Bentley-Mc. Ilroy 3 -way partitioning Repeat until i and j pointers cross. • Scan

Bentley-Mc. Ilroy 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. p P lo P P C q A D B i V Y j Z W X P P hi 59

Bentley-Mc. Ilroy 3 -way partitioning Repeat until i and j pointers cross. • Scan

Bentley-Mc. Ilroy 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. p P lo P P C q A D B V Y i j Z W X P P hi 60

Bentley-Mc. Ilroy 3 -way partitioning Repeat until i and j pointers cross. • Scan

Bentley-Mc. Ilroy 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. p P lo P P C q A D B V i j Y Z W X P P hi 61

Bentley-Mc. Ilroy 3 -way partitioning Repeat until i and j pointers cross. • Scan

Bentley-Mc. Ilroy 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. p P lo P P C q A D B V j i pointers cross Y Z W X P P hi 62

Bentley-Mc. Ilroy 3 -way partitioning Afterwards, swap equal keys to the center. • Scan

Bentley-Mc. Ilroy 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]. p P lo P P q C A D B V j i Y exchange a[j] with a[p] Z W X P P hi 63

Bentley-Mc. Ilroy 3 -way partitioning Afterwards, swap equal keys to the center. • Scan

Bentley-Mc. Ilroy 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]. p P lo P q B C A D j P V Y i exchange a[j] with a[p] Z W X P P hi 64

Bentley-Mc. Ilroy 3 -way partitioning Afterwards, swap equal keys to the center. • Scan

Bentley-Mc. Ilroy 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]. p P lo q D B C A j P P V Y i exchange a[j] with a[p] Z W X P P hi 65

Bentley-Mc. Ilroy 3 -way partitioning Afterwards, swap equal keys to the center. • Scan

Bentley-Mc. Ilroy 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]. q A lo D B C j P P P V Y i exchange a[i] with a[q] Z W X P P hi 66

Bentley-Mc. Ilroy 3 -way partitioning Afterwards, swap equal keys to the center. • Scan

Bentley-Mc. Ilroy 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]. q A lo D B C j P P Y i exchange a[i] with a[q] Z W X V P hi 67

Bentley-Mc. Ilroy 3 -way partitioning Afterwards, swap equal keys to the center. • Scan

Bentley-Mc. Ilroy 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]. A lo D B C P P j P Z i 3 -way partitioned W X V Y hi 68