L : ( 2, 4 )
G : ( 7, 6 )
L : ( )
G : ( 10 )
x = 4
x = 6
L : ( 2 )
G : ( )
L : ( )
G : ( 7 )
2 4 5 6 7 8 9 10
Implementation
 Implement an "in place quicksort where extra space is not needed to store
L
and
G
at each step. Instead use only
s
.
 Use array subranges to represent each set.
 The last element in the array is the
pivot
.
( 85, 24, 63, 45, 17, 31, 96, 50 ) x = 50.

To separate ( divide )
s
into
L
and
G
, we use
l
and
r
and iterate
l
form left to right until and element greater than
the
pivot
is found. We iterate
r
from right to left until an element is found that is less than the
pivot
. Then, if both
elements are found, swap them.
 The process continues until
l
and
r
cross
.
85, 24, 63, 45, 17, 31, 96, 50
x
=
50
l
=
85
(>50)
r
= 31 (<50)
31, 24, 63, 45, 17, 85, 96, 50

>
l
r
<

31, 24, 17, 45, 63, 85, 96, 50



>
l
r
<


CROSS
 Swap
pivot
with element pointed to by
l
.
 Swap 63 and 50.
31, 24, 17, 45,
50,
85, 96, 63
// Sort a subrange of integers in an array s
substring
// at "left" ending at "right"
void quickSortSubrange ( int * s, int left, int
right )
{
// return if subrange has one element
if ( left > right )
{
return;
}
// Use last element as pivot
int x = s[ right ];
// Divide s into L and G
int l = left;
int r = right  1;