Quick sorting is an application of grouping and recursion. The time complexity of the algorithm is O (nlogn ). The recursive depth of the algorithm is close to that of logn. Therefore, the required unit of work is O (logn ).
// Quick sorting
# Include <iostream. h>
Int count = 0;
Void swap (int & a, int & B)
{
Int temp;
Temp =;
A = B;
B = temp;
}
Int split (int array [], int low, int high)
{
Int I = low; // Save the position of the pivot element. The initial value is low.
Int j;
Int x = array [low];
For (j = low + 1; j <= high; j ++)
{
If (array [j] <= x)
{
I ++;
If (I! = J)
{
Swap (array [I], array [j]);
}
}
}
Swap (array [low], array [I]);
Return I;
}
Void print (int array [], int n)
{
For (int I = 0; I <8; I ++)
{
Cout <array [I] <"";
}
Cout <endl;
}
Void quick_sort (int array [], int low, int high)
{
Int k;
If (low {
K = split (array, low, high );
Quick_sort (array, low, k-1 );
Quick_sort (array, k + 1, high );
}
}
Void main ()
{
Int array [] = {5, 8, 4, 9, 3, 6, 7, 2 };
Quick_sort (array, 0, 7 );
Print (array, 7 );
}