In the introduction to algorithms, the reference element is selected as the final one, and the data structure textbook I used is the first;
However, the principle is the same. The elements in array a are divided into three parts by using the Qartion (int a [], int l, int r) function, which is smaller than a [r, larger than a [r], a [r]; www.2cto.com
Finally, the location of the base element is returned;
[Cpp]
# Include <stdio. h>
# Include <iostream>
# Include <string. h>
# Include <algorithm>
# Include <math. h>
Using namespace std;
Const int N = 99;
Int Qartion (int a [], int l, int r)
{
Int tmp = a [r];
Int I = L-1;
For (int j = l; j <= R-1; j ++)
{
If (a [j] <= tmp)
{
I ++;
Swap (a [I], a [j]);
}
}
Swap (a [I + 1], a [r]);
Return I + 1;
}
Void Qsort (int a [], int l, int r)
{
If (l <r)
{
Int p = Qartion (a, l, r );
Qsort (a, l, P-1 );
Qsort (a, p + 1, r );
}
}
Int main ()
{
Int n;
While (~ Scanf ("% d", & n ))
{
Int a [100];
For (int I = 0; I <n; I ++)
{
Cin> a [I];
}
Qsort (a, 0, n-1 );
For (int I = 0; I <n; I ++)
{
Cout <a [I] <"";
}
Cout <endl;
}
Return 0;
}
Author: Java_beginer1