选择排序:选择排序是一种简单直观的排序算法,无论什么数据进去都是 O(n²) 的时间复杂度。所以用到它的时候,数据规模越小越好。唯一的好处可能就是不占用额外的内存空间了吧。
算法思路:
首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置。
再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的末尾。
重复第二步,直到所有元素均排序完毕。
动图演示:
c#代码实现:
public void Num(int[] list)
{
for (int i = 0; i < list.Length - 1; ++i)
{
numone = i;
for (int j = i + 1; j < list.Length; ++j)
{
if (list[j] < list[numone])
{
numone = j;
}
int numtwo = list[numone];
list[numone] = list[i];
list[i] = numtwo;
}
}
}
static void Main(string[] args)
{
int[] count = new int[] { 88, 666,666, 456, 8, 999, 33, 123, 985, 211, 520 };
Program po = new Program();
po.Num(count);
for (int i = 0; i < count.Length; i++)
{
Console.WriteLine(count[i]);
}
Console.ReadKey();
}
运行结果:
转载于:https://github.com/hustcc/JS-Sorting-Algorithm
原文:https://www.cnblogs.com/m962263807/p/13289289.html