catalog
Quick Sorting
The worst-case Scenario for quick sorting is O(n2), such as quick sorting of sequential sequences, but its average expected time is O(nlogn).
Steps
- Select an element from the sequence and call it a pivot.
- Reorder the sequence, placing all items smaller than the pivot before the pivot and those larger than the pivot after the pivor. This is called partitioning operation.
- Recursively sort subsequence that is less than the pivot and subsequence that is greater than the pivot.
Implementation
void quick_sort(int q[], int l, int r)
{
if (l >= r) return;
int i = l - 1, j = r + 1, x = q[l + r >> 1];
while (i < j)
{
do i++; while (q[i] < x);
do j--; while (q[j] > x);
if (i < j) swap(q[i], q[j]<