筆記----算法導論(一)

算法導論(一)

個人對算法導論(第三版)一書筆記記錄


第一章

  1. 什麼是算法?

    這裏寫圖片描述

    將輸入轉變成輸出所需要的一系列的計算步驟

第二章

  1. 插入排序

    這裏寫圖片描述

序列 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

發佈了22 篇原創文章 · 獲贊 7 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章