今天主要是將排序算法中最簡單的C#冒泡排序
C#冒泡排序代碼
class SortingAlgorithms
{
private int[] arr;
private int upper;
private int numElement;
//初始化數組
public SortingAlgorithms(int size)
{
arr = new int[size];
upper = size
numElement =
}
//給數組插入元素
public void Insert(int item)
{
arr[numElement] = item;
numElement++;
}
//打印數組元素
public void DisplayElement()
{
for (int i =
{
Console
}
Console
}
//冒泡排序
public void BubbleSort()
{
int Temp;
//外循環是限制一次冒泡排序比較的元素個數
for (int Outer = upper; Outer >=
{
//一次冒泡排序比較
for (int Inner =
{
//排序過程
if (arr[Inner] > arr[Inner +
{
Temp = arr[Inner];
arr[Inner] = arr[Inner +
arr[Inner +
}
}
}
//this
}
}
一下是調用算法的過程
static void Main(string[] args)
{
SortingAlgorithms MyArray = new SortingAlgorithms(
Random rnd = new Random(
for (int i =
{
MyArray
}
Console
MyArray
//進行排序
MyArray
Console
//打印排序後的元素
MyArray
}
From:http://tw.wingwit.com/Article/program/ASP/201311/21647.html