• <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 閱讀(262) 評論(0)  編輯 收藏 引用
            Google PageRank 
Checker - Page Rank Calculator
            国产人久久人人人人爽| 久久久久AV综合网成人| 91久久香蕉国产熟女线看| 91亚洲国产成人久久精品| 香蕉久久永久视频| 久久99亚洲网美利坚合众国| 久久国产乱子精品免费女| 青青热久久国产久精品| 久久精品人人做人人爽97| 7国产欧美日韩综合天堂中文久久久久 | 国产精品福利一区二区久久| 91精品国产91久久久久久青草 | 精品蜜臀久久久久99网站| 大蕉久久伊人中文字幕| 久久国内免费视频| 青青草国产成人久久91网| 欧美伊人久久大香线蕉综合| 精品久久久久久国产| 色婷婷久久综合中文久久一本| 亚洲女久久久噜噜噜熟女| 99久久人人爽亚洲精品美女| AV无码久久久久不卡蜜桃| 久久最近最新中文字幕大全| 久久综合亚洲色一区二区三区| 久久婷婷国产麻豆91天堂| 亚洲国产另类久久久精品小说 | 国产香蕉97碰碰久久人人| 国产成人精品免费久久久久| 亚洲日本va午夜中文字幕久久| 国产精品99久久不卡| 久久久精品免费国产四虎| 欧洲人妻丰满av无码久久不卡| 精品综合久久久久久98| 久久久久久久久久久免费精品 | 香蕉久久夜色精品国产2020| 国产精品内射久久久久欢欢| 青青青国产精品国产精品久久久久 | 久久综合欧美成人| 久久综合久久综合九色| 91麻精品国产91久久久久| 久久午夜电影网|