Quick sorting is the most common sorting method in practical programming applications.
He has excellent performance.
The worst case time complexity is O (n square meters)
The average time complexity is O (n logn) and has a very small coefficient.
And the space complexity is very small, that is, O (n)
But thisAlgorithmIt is also difficult to understand...
The following is a quick algorithm that uses the last element as the token.
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;
Using System. runtime. interopservices;
Namespace Introducetoalgorithm
{
Public Class Quicksort
{
Public Void Sort ( Int [], Int Startindex = 1 , Int Endindex = - 1 )
{
If (Endindex = - 1 )
{
Endindex = A. length;
}
If (Startindex < Endindex)
{
Int Q = Partition (A, startindex, endindex );
Sort (A, startindex, Q - 1 );
Sort (A, Q + 1 , Endindex );
}
}
Public Int Partition ( Int [], Int Startindex, Int Endindex)
{
Int X = A [endindex - 1 ];
Int I = Startindex - 1 ;
For ( Int J = Startindex; j <= Endindex - 1 ; J ++ )
{
If (A [J - 1 ] <= X)
{
I = I + 1 ;
Exchange (A, I - 1 , J - 1 );
}
// A. Print ();
}
Exchange (A, I, endindex - 1 );
// A. Print ();
Return I + 1 ;
}
Public Void Exchange ( Int [], Int I, Int I2)
{
Int Val = A [I];
A [I] = A [I2];
A [I2] = Val;
}
}
}
PS: this algorithm is hard to understand when it is implemented...CodeSee... although there are few codes