方法一:
Array.Sort();
代码:
using System;
namespace 排序
{
class Program
{
static void Main(string[] args)
{
string str = Console.ReadLine();
string[] sttr = str.Split(" ");
int[] a = new int[sttr.Length];
for(int i=0;i<sttr.Length;i++)
{
a[i] = Convert.ToInt32(sttr[i]);
}
Array.Sort(a);
foreach(int m in a)
{
Console.Write(m + " ");
}
}
}
}
运行结果:
方法二:
冒泡排序
代码:
using System;
namespace 冒泡排序
{
class Program
{
static void Main(string[] args)
{
string str = Console.ReadLine();
string[] sttr = str.Split(" ");
int[] a = new int[sttr.Length];
for(int i=0;i<sttr.Length;i++)
{
a[i] = Convert.ToInt32(sttr[i]);
}
for (int k = 0; k < a.Length-1; k++)
{
bool isChange = false;
for (int j=0;j<a.Length-1-k;j++)
{
if (a[j] >a[j+1])
{
int t = a[j];
a[j] = a[j+1];
a[j+1]= t;
isChange = true;
}
}
if (isChange == false)
break;
}
foreach(int m in a)
{
Console.Write(m + " ");
}
}
}
}
运行结果: