• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            bon

              C++博客 :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
              46 Posts :: 0 Stories :: 12 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(2)

            我參與的團隊

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            Recently, I began to peruse the CLRS (Introduction to Algorithms).

            Now I would like to scan all basic algorithms, especially sorting and searching.

            Let me first present a classic sorting algorithm - merge sort. Here is my code. Before I reach here, some mistakes are made, thus I note these "pitfalls" in the code

            #include <stdlib.h>
            #include <stdio.h>
            #define MAX 1e9    // the biggest possible value of a int number is 2^31 - 1 which is approximately 10^9
            #define SIZE 10

            int *a;
            int *b;
            int *c;

            // merge [p, q] and [q+1, r], where within each range number are sorted
            void merge(int p, int q, int r)
            {
                int k;
                int length = r - p +1;            // the length the range to be merge
                for (k = 0; k < q - p + 1; k++) {
                    b[k] = a[p + k];             // copy number in a[p, q] to b
                }
                b[k] = MAX;             // b[k] = MAX, not b[k+1]=MAX
                for (k = 0; k < r - q; k++) {
                    c[k] = a[q + 1 + k];             // copy number in a[q+1, r] to c
                }
                c[k] = MAX;             // c[k] = MAX, not c[k+1]=MAX

                /* BEGIN merging */
                int i = 0;
                int j = 0;
                for (k=0;k<length;k++) {             // do exactly length times of copy
                    if (b[i] < c[j]) {
                        a[p + k] = b[i++];          // be careful! a[p, r] is a whole range now, and watch out the base "p"
                    } else {
                        a[p + k] = c[j++];
                    }
                }
            }

            void merge_sort(int l, int u)
            {
                if (l == u) return;             // when to stop recursion? only one number needs no sorting
                int m = (l + u)/2;
                merge_sort(l, m);
                merge_sort(m + 1, u);
                merge(l, m, u);
            }

            int main()
            {
                a = (int*)malloc(SIZE * sizeof(int));
                b = (int*)malloc(SIZE * sizeof(int));          // cache, avoid many "malloc" in the merge function
                c = (int*)malloc(SIZE * sizeof(int));          // this trick is from "Programming Pearls"
                int i;
                for (i = 0; i < SIZE; i++) {
                    a[i] = SIZE - i;
                }
                merge_sort(0, SIZE - 1);                    // watch out the range
                for (i = 0; i < SIZE; i++) {
                    printf("%d\n", a[i]);
                }
                return 1;
            }

            posted on 2009-04-30 13:57 bon 閱讀(258) 評論(0)  編輯 收藏 引用

            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            Google PageRank 
Checker - Page Rank Calculator
            国产精品综合久久第一页| 国产精品欧美亚洲韩国日本久久| 久久九九青青国产精品| 97久久国产露脸精品国产| 青青久久精品国产免费看| 国产精品狼人久久久久影院| 欧美精品久久久久久久自慰| 久久久久久久综合狠狠综合| 人妻系列无码专区久久五月天| 国产精品美女久久久久AV福利| 久久青青草原综合伊人| 国产一级做a爰片久久毛片| 2021精品国产综合久久| 国产美女久久精品香蕉69| 久久精品a亚洲国产v高清不卡| 久久久无码精品亚洲日韩按摩| 国产精品99久久99久久久| 久久久久亚洲AV成人片| 久久99精品国产麻豆宅宅| 精品国产91久久久久久久| 奇米综合四色77777久久| 99久久婷婷免费国产综合精品| 精品久久久久久久久午夜福利| 日本道色综合久久影院| 国产精品丝袜久久久久久不卡| 国内精品久久久久影院免费| 99久久无码一区人妻| 蜜桃麻豆www久久国产精品| 精品久久久一二三区| 性欧美丰满熟妇XXXX性久久久| 久久精品国产亚洲精品2020| 国产人久久人人人人爽 | 伊人久久大香线蕉精品不卡| 亚洲色婷婷综合久久| 久久99国产综合精品女同| 青青青国产成人久久111网站| 久久久久国产精品麻豆AR影院| 久久精品久久久久观看99水蜜桃| 国产成人久久精品一区二区三区| 99久久人妻无码精品系列蜜桃| 久久嫩草影院免费看夜色|