• <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++博客 :: 首頁 :: 聯系 :: 聚合  :: 管理
              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 閱讀(261) 評論(0)  編輯 收藏 引用
            Google PageRank 
Checker - Page Rank Calculator
            精品久久久久久无码人妻热 | 国产精品99久久99久久久| 久久国产热这里只有精品| 久久综合狠狠综合久久97色| 久久综合鬼色88久久精品综合自在自线噜噜 | 91精品国产91久久久久福利| AAA级久久久精品无码区| 麻豆精品久久久久久久99蜜桃 | 国产午夜精品理论片久久影视 | 99久久免费国产特黄| 93精91精品国产综合久久香蕉| 久久伊人五月天论坛| 国产精品久久永久免费| 欧美日韩精品久久久久| 久久精品国产亚洲Aⅴ蜜臀色欲| 亚洲va中文字幕无码久久| 久久久久黑人强伦姧人妻| 国产精品久久久久国产A级| 久久精品国产男包| 久久久久久极精品久久久| 国内精品久久久人妻中文字幕| 亚洲午夜久久久| 狠狠色丁香婷婷综合久久来来去| 婷婷久久久亚洲欧洲日产国码AV| 免费一级做a爰片久久毛片潮| 久久精品国产久精国产| 麻豆一区二区99久久久久| 久久精品国产男包| 伊人久久大香线蕉成人| 亚洲Av无码国产情品久久| 国产高潮国产高潮久久久91 | 观看 国产综合久久久久鬼色 欧美 亚洲 一区二区 | 思思久久精品在热线热| 久久久久国色AV免费观看| 国产精品xxxx国产喷水亚洲国产精品无码久久一区 | 国产亚洲精品久久久久秋霞| 亚洲国产精品成人久久蜜臀 | 亚洲国产精品久久久久| 精品999久久久久久中文字幕| 99精品久久精品| 国产AV影片久久久久久|