using System;
using SystemCollectionsGeneric;
using SystemText;
namespace BubbleSort
{
class Program
{
static void Main(string[] args)
{
TestArray nums = new TestArray();
#region 初始化數組
Random rnd = new Random();
for (int num = ; num < ; num++)
{
numsInsert(rndNext());
}
#endregion
ConsoleWriteLine(Before Sorting: );
numsDisplayElements();
ConsoleWriteLine(Durring Sorting: );
numsInsertionSort();
ConsoleWriteLine(After Sorting: );
numsDisplayElements();
ConsoleReadLine();
}
}
}
public class TestArray
{
private int[] arr;
private int upper;
private int numElements;
public TestArray(int size)
{
arr = new int[size];
upper = size ;
numElements = ;
}
public void Insert(int item)
{
arr[numElements] = item;
numElements++;
}
public void DisplayElements()
{
for (int num = ; num <= upper; num++)
{
ConsoleWrite(arr[num]+ );
}
ConsoleWriteLine();
}
public void Clear()
{
for (int num = ; num <= upper; num++)
{
arr[num] = ;
}
numElements = ;
}
//插入排序算法
public void InsertionSort()
{
int inner temp;
for(int outer=;outer<=upper;outer++)
{
inner=outer;
temp=arr[outer];
while(inner> && arr[inner]>=temp)
{
arr[inner]=arr[inner];
inner=;
}
arr[inner]=temp;
thisDisplayElements();
}
}
}
From:http://tw.wingwit.com/Article/program/net/201311/13854.html