Now there is a list, and the K to M elements of the List need to be fully arranged and output.
Basic Idea:
(1) When K = M, that is, when the sorting column does not exist, the value from 0 to M is directly output.
(2) When K> M, sort each element in the sequence to the K-bit, and then sort all K + 1 to M items.
The algorithm is as follows:
Protected void button#click (object sender, EventArgs e)
{
List <int> list = new List <int> {1, 2, 3, 4, 5, 6, 7, 8, 9 };
Perm (list, 5, 8 );
}
/// <Summary>
/// List all
/// </Summary>
/// <Param name = "list"> List to be sorted </param>
/// <Param name = "k"> Start position </param>
/// <Param name = "m"> end position </param>
Protected void Perm (List <int> list, int k, int m)
{
If (k = m)
{
For (int I = 0; I <= m; I ++)
{
Response. Write (list [I]. ToString () + "");
}
Response. Write ("<br> ");
}
Else
{
For (int I = k; I <= m; I ++)
{
Swap (ref list, k, I );
Perm (list, k + 1, m );
Swap (ref list, k, I );
}
}
}
Private void Swap (ref List <int> list, int a, int B)
{
Int temp = list [a];
List [a] = list [B];
List [B] = temp;
}