冒泡
- 比较相邻的元素。如果第一个比第二个大,就交换他们两个。
- 对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对。这步做完后,最后的元素会是最大的数。
- 针对所有的元素重复以上的步骤,除了最后一个。
- 持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要比较。
public static void Main(string[] args)
{
int[] num = { , , , , , , , , , , , , , , , , , , , , , , , , };
for (int i = ; i < num.Length - ; i++)
{
for (int j = num.Length - ; j > i; j--)
{
if (num[j] < num[j - ])
{
int temp = num[j];
num[j] = num[j - ];
num[j - ] = temp;
}
}
}
Console.WriteLine("排序后的数组:");
for (int k = ; k < num.Length; k++)
{
Console.Write("{0},", num[k]);
} Console.ReadKey();
}
C#普通排序:
int[] sort = new int[] { , , , , , , , , , , , , }; // 输入一个数组
foreach (var min in sort.OrderBy(c => c))
Console.WriteLine(min);
Console.ReadKey();