1、冒泡排序
class Program
{
static void Main(string[] args)
{
SortHelper sorter = new SortHelper();
int[] array = { 8, 1, 4, 7, 3 };
sorter.BubbleSort(array);
foreach (int i in array)
{
Console.WriteLine("{0}", i);
}
Console.WriteLine();
Console.ReadKey();
}
}
public class SortHelper
{
//冒泡排序
public void BubbleSort(int[] array)
{
int length = array.Length;
for (int i = 0; i < length; i++)
{
for (int j = i + 1; j < length; j++)
{
//对两个元素进行交换
if (array[j] < array[i])
{
int temp = array[j];
array[j] = array[i];
array[i] = temp;
}
}
}
}
}