算法导论(一)
个人对算法导论(第三版)一书笔记记录
第一章
什么是算法?
将输入转变成输出所需要的一系列的计算步骤。
第二章
插入排序
序列 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