冒泡排序:前后两个两两比较,根据需求(从小到大,从大到小)比较
时间复杂度:O(n²) 稳定性:稳定 空间复杂度 O(1)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 冒泡排序1
{
class Program
{
static void Main(string[] args)
{
int[] x = new int[5] { 1, 6, 8, 2, 4 };
for (int i = 0; i < x.Length-1; i++)
{
for (int j = 0; j < x.Length-i-1; j++)
{
if (x[j]>x[j+1]) //从小到大排序
{
int temp = x[j];
x[j] = x[j + 1];
x[j + 1] = temp;
}
}
}
for (int i = 0; i < x.Length; i++)
{
Console.WriteLine("{0}", x[i]);
}
Console.ReadKey();
}
}
}