The basic idea of quick sorting is based on the divide and conquer policy. If the input sub-sequence AP. Ar is small enough, the sub-sequence is sorted directly. Otherwise, the sub-sequence is processed in three steps:
Divide ):Set the input sequence AP .. ar is divided into two non-empty subsequence APS .. AQ and AQ + 1 .. ar To make the ap .. the value of any element in AQ is not greater than AQ + 1 .. the value of any element in Ar.
Recursive solution (conquer ):Recursively sorts P. aq and AQ + 1. ar.
Merge (merge ):Because the sorting of the two sub-sequences obtained by splitting is performed in place .. AQ and AQ + 1 .. after the AR is sorted, no computing AP is required .. ar is sorted.
This solution process is in line with the division and Control Law. Therefore, quick sorting is one of the classic application instances of the divide and conquer method.
Using system;
Namespace vcquicksort
{
/// <Summary>
/// Classquicksort quick sorting.
/// </Summary>
Public class quicksort
{
Public quicksort ()
{
}
Private void swap (ref int I, ref Int J)
// Swap two integer
{
Int T;
T = I;
I = J;
J = T;
}
Public void sort (INT [] list, int low, int high)
{
If (high <= low)
{
// Only one element in array list
// So it do not need sort
Return;
}
Else if (high = low + 1)
{
// Means two elements in array list
// So we just compare them
If (list [low]> list [High])
{
// Exchange them
Swap (Ref List [low], Ref List [High]);
Return;
}
}
// More than 3 elements in the arrary list
// Begin quicksort
Myquicksort (list, low, high );
}
Public void myquicksort (INT [] list, int low, int high)
{
If (low
{
Int partition = partition (list, low, high );
Myquicksort (list, low, skip-1 );
Myquicksort (list, tables + 1, high );
}
}
Private int partition (INT [] list, int low, int high)
{
// Get the attributes of the arrary list
Int timeout;
Dependencies = list [low];
While (low
{
While (low <High & list [High]> = running)
{
High --;
}
If (low! = High)
{
Swap (Ref List [low], Ref List [High]);
Low ++;
}
While (low <High & list [low] <= lower)
{
Low ++;
}
If (low! = High)
{
Swap (Ref List [low], Ref List [High]);
High --;
}
}
Return low;
}
}
}