算法導論(一)
個人對算法導論(第三版)一書筆記記錄
第一章
什麼是算法?
將輸入轉變成輸出所需要的一系列的計算步驟。
第二章
插入排序
序列 4、3、1、2
經過步驟 a 、b、c完成插入排序,
僞代碼如下:
c/c++代碼如下:
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();
}
參考:
1. http://www.cnblogs.com/kkun/archive/2011/11/23/2260265.html