• <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)

            我參與的團(tuán)隊

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            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
            国产精品久久久久久久| 久久精品综合网| 成人精品一区二区久久| 久久无码人妻精品一区二区三区| 久久无码国产| 国产成人无码久久久精品一| 久久精品二区| 99久久精品免费看国产一区二区三区| 久久狠狠高潮亚洲精品| 久久免费视频一区| 成人免费网站久久久| 国产色综合久久无码有码| 亚洲午夜久久影院| 久久婷婷五月综合国产尤物app| 精品免费久久久久国产一区| 性欧美大战久久久久久久久 | 国产激情久久久久久熟女老人| AV色综合久久天堂AV色综合在| 欧美日韩精品久久久免费观看| 久久99精品久久久久久| 亚洲精品高清国产一线久久| 色综合久久天天综线观看| 亚洲狠狠久久综合一区77777 | 成人午夜精品久久久久久久小说| 97久久婷婷五月综合色d啪蜜芽 | 青青热久久国产久精品| 91精品国产综合久久香蕉| 久久国产亚洲精品无码| 无码人妻久久一区二区三区免费| 四虎久久影院| 亚洲精品无码久久不卡| 久久亚洲AV永久无码精品| 久久久久九国产精品| 国产综合精品久久亚洲| 久久免费国产精品| 日韩人妻无码一区二区三区久久99 | 国产成人久久精品二区三区| 无码AV波多野结衣久久| 久久精品蜜芽亚洲国产AV| 久久精品亚洲日本波多野结衣| 久久久久久久97|