3-Way QuickSort - GeeksforGeeks
In simple QuickSort algorithm, we select an element as pivot, partition the array around pivot and recur for subarrays on left and right of pivot.
Consider an array which has many redundant elements. For example, {1, 4, 2, 4, 2, 4, 1, 2, 4, 1, 2, 2, 2, 2, 4, 1, 4, 4, 4}. If 4 is picked as pivot in Simple QuickSort, we fix only one 4 and recursively process remaining occurrences.
http://geeksquiz.com/quick-sort/
The key process in quickSort is partition(). Target of partitions is, given an array and an element x of array as pivot, put x at its correct position in sorted array and put all smaller elements (smaller than x) before x, and put all greater elements (greater than x) after x. All this should be done in linear time.
Read full article from 3-Way QuickSort - GeeksforGeeks
In simple QuickSort algorithm, we select an element as pivot, partition the array around pivot and recur for subarrays on left and right of pivot.
Consider an array which has many redundant elements. For example, {1, 4, 2, 4, 2, 4, 1, 2, 4, 1, 2, 2, 2, 2, 4, 1, 4, 4, 4}. If 4 is picked as pivot in Simple QuickSort, we fix only one 4 and recursively process remaining occurrences.
The idea of 3 way QuickSort is to process all occurrences of pivot.
In 3 Way QuickSort, an array arr[l..r] is divided in 3 parts: a) arr[l..i] elements less than pivot. b) arr[i+1..j-1] elements equal to pivot. c) arr[j..r] elements greater than pivot.
/* This function partitions a[] in three parts
a) a[l..i] contains all elements smaller than pivot
b) a[i+1..j-1] contains all occurrences of pivot
c) a[j..r] contains all elements greater than pivot */
void
partition(
int
a[],
int
l,
int
r,
int
&i,
int
&j)
{
i = l-1, j = r;
int
p = l-1, q = r;
int
v = a[r];
while
(
true
)
{
// From left, find the first element greater than
// or equal to v. This loop will definitely terminate
// as v is last element
while
(a[++i] < v);
// From right, find the first element smaller than or
// equal to v
while
(v < a[--j])
if
(j == l)
break
;
// If i and j cross, then we are don
if
(i >= j)
break
;
// Swap, so that smaller goes on left greater goes on right
swap(a[i], a[j]);
// Move all same left occurrence of pivot to beginning of
// array and keep count using p
if
(a[i] == v)
{
p++;
swap(a[p], a[i]);
}
// Move all same right occurrence of pivot to end of array
// and keep count using q
if
(a[j] == v)
{
q--;
swap(a[j], a[q]);
}
}
// Move pivot element to its correct index
swap(a[i], a[r]);
// Move all left same occurrences from beginning
// to adjacent to arr[i]
j = i-1;
for
(
int
k = l; k < p; k++, j--)
swap(a[k], a[j]);
// Move all right same occurrences from end
// to adjacent to arr[i]
i = i+1;
for
(
int
k = r-1; k > q; k--, i++)
swap(a[i], a[k]);
}
// 3-way partition based quick sort
void
quicksort(
int
a[],
int
l,
int
r)
{
if
(r <= l)
return
;
int
i, j;
// Note that i and j are passed as reference
partition(a, l, r, i, j);
// Recur
quicksort(a, l, j);
quicksort(a, i, r);
}
http://geeksquiz.com/quick-sort/
The key process in quickSort is partition(). Target of partitions is, given an array and an element x of array as pivot, put x at its correct position in sorted array and put all smaller elements (smaller than x) before x, and put all greater elements (greater than x) after x. All this should be done in linear time.
/* This function takes last element as pivot, places the pivot element at its
correct position in sorted array, and places all smaller (smaller than pivot)
to left of pivot and all greater elements to right of pivot */
int
partition (
int
arr[],
int
l,
int
h)
{
int
x = arr[h];
// pivot
int
i = (l - 1);
// Index of smaller element
for
(
int
j = l; j <= h- 1; j++)
{
// If current element is smaller than or equal to pivot
if
(arr[j] <= x)
{
i++;
// increment index of smaller element
swap(&arr[i], &arr[j]);
// Swap current element with index
}
}
swap(&arr[i + 1], &arr[h]);
return
(i + 1);
}
/* arr[] --> Array to be sorted, l --> Starting index, h --> Ending index */
void
quickSort(
int
arr[],
int
l,
int
h)
{
if
(l < h)
{
int
p = partition(arr, l, h);
/* Partitioning index */
quickSort(arr, l, p - 1);
quickSort(arr, p + 1, h);
}
}