經典排序算法 – 插入排序Insertion sort
插入排序就是每一步都将一個待排資料按其大小插入到已經排序的資料中的适當位置,直到全部插入完畢。
插入排序方法分直接插入排序和折半插入排序兩種,這裡隻介紹直接插入排序,折半插入排序留到“查找”内容中進行。
圖1示範了對4個元素進行直接插入排序的過程,共需要(a),(b),(c)三次插入。

/// <summary>
/// 插入排序
/// </summary>
/// <param name="unsorted"></param>
static void insertion_sort(int[] unsorted)
{
for (int i = 1; i < unsorted.Length; i++)
{
if (unsorted[i - 1] > unsorted[i])
{
int temp = unsorted[i];
int j = i;
while (j > 0 && unsorted[j - 1] > temp)
{
unsorted[j] = unsorted[j - 1];
j--;
}
unsorted[j] = temp;
}
}
}
static void Main(string[] args)
{
int[] x = { 6, 2, 4, 1, 5, 9 };
insertion_sort(x);
foreach (var item in x)
{
if (item > 0)
Console.WriteLine(item + ",");
}
Console.ReadLine();
}
http://www.cnblogs.com/kkun/archive/2011/11/23/2260265.html