原理是 :找到初始最大的一位数 确定其所在的位置 保持最大数所在位置不变 并得到此数字的索引 最后将其位置与当前未排序的最后一位数字交换位置
struct TestChoice
{
public void Choice(int[] ints)
{
for (int m = 0; m < ints.Length; m++)
{
int index = 0;
for (int n = 1; n < ints.Length - m; n++)
{
if (ints[index] < ints[n])
{
index = n;
}
}
//如果恰好此次将要排序的最大一位数字 正好在最后 则不用排序
if (index != ints.Length - 1 - m)
{
int temp = ints[index];
ints[index] = ints[ints.Length - 1 - m];
ints[ints.Length - 1 - m] = temp;
}
}
}
}
internal class ChoiceSort
{
static void Main(string[] args)
{
int[] ints = { 1, 9, 5, 2, 6, 7 };
TestChoice testChoice;
testChoice.Choice(ints);
for (int i = 0; i < ints.Length; i++)
{
Console.WriteLine(ints[i]);
}
}
}