//第一种方法
//从小到大排列
int[] sort ={ 3, 5, 7, 1, 2, 4, 8, 9, 11, 10 };
for (int j = 0; j < sort.Length - 1; j++)
{
for (int i = 0; i < sort.Length-1; i++)
{
if (sort[i] > sort[i + 1])
{
int temp = sort[i];//声明一个变量temp
sort[i] = sort[i + 1];
sort[i + 1] = temp;
}
}
}
//打印结果
for (int i = 0; i < sort.Length; i++)
{
Console.Write(sort[i] + " ");
}
Console.WriteLine("\n用foreach打印结果");
foreach (int item in sort)
{
Console.Write(item + " ");
}
//第二种方法(道理都是一样)(推荐)
for (int i = 0; i <sort.Length; i++)
{
for (int j = i + 1; j < sort.Length; j++)
{
if (sort[i]>sort[j])
{
//声明一个变量temp
int temp = sort[i];
sort[i] = sort[j];
sort[j] = temp;
}
}
}
//第二种方法打印结果
Console.WriteLine("\n第二种打印结果");
foreach (int item in sort)
{
Console.Write(item+" ");
}
Console.ReadKey();