冒泡排序方法:
int[] a = { 5, 1, 7, 2, 3 };
for (int i = 0; i < a.Length; i++)
{
for (int j = 0; j < a.Length - i - 1; j++)
{
if (a[j] > a[j + 1])
{
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
Console.WriteLine("升序排序后的结果为:");
foreach (int b in a)
{
Console.Write(b + " ");
}
Console.WriteLine();
Console.ReadKey();
排序方法:
int[] a = { 8, 9, 7, 54, 1 };
Array.Sort(a);
Console.WriteLine("排序后的结果为:");
foreach (int b in a)
{
Console.Write(b + " ");
}
Console.WriteLine();
Console.ReadKey();