冒泡排序(Bubble Sort),是一种计算机科学领域的较简单的排序算法。
C#语言
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1 数组排序
{
class Program
{
static void Main(string[] args)
{
int temp = 0;
int[] arr = { 23, 44, 66, 76, 98, 11, 3, 9, 7 };
//显示排序前的数组
Console.WriteLine(“排序前的数组:”);
foreach (int item in arr)
{
Console.Write(item + ” ”);
}
Console.WriteLine();
for (int i = 0; i < arr.Length — 1; i++)
{
//将大的数字移到数组的arr.Length-1‑i
for (int j = 0; j < arr.Length — 1 — i; j++)
{
if (arr[j] > arr[j + 1])
{
temp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = temp;
}
}
}
Console.WriteLine(“排序后的数组:”);
foreach (int item in arr)
{
Console.Write(item + ” ”);
}
Console.WriteLine();
Console.ReadKey();
}
}
}