• <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 閱讀(263) 評論(0)  編輯 收藏 引用
            Google PageRank 
Checker - Page Rank Calculator
            亚洲中文字幕无码久久2020 | 久久久久成人精品无码中文字幕 | 精品国产青草久久久久福利| 一本一道久久a久久精品综合| 久久久久久噜噜精品免费直播 | 国产69精品久久久久99尤物| 久久97久久97精品免视看秋霞| 久久狠狠一本精品综合网| 性高湖久久久久久久久AAAAA| 国内精品伊人久久久影院| 久久99精品久久久久久久久久 | 热re99久久精品国99热| 国产成人精品久久亚洲高清不卡 国产成人精品久久亚洲高清不卡 国产成人精品久久亚洲 | 岛国搬运www久久| 久久久久亚洲av综合波多野结衣| 久久亚洲中文字幕精品有坂深雪 | 日韩中文久久| 久久er热视频在这里精品| 亚洲国产日韩综合久久精品| 狠狠干狠狠久久| 日韩av无码久久精品免费| 少妇久久久久久被弄到高潮| 国产精品美女久久久久久2018| 亚洲AⅤ优女AV综合久久久| 国内精品久久九九国产精品| 一本久久精品一区二区| 国产精自产拍久久久久久蜜| 国内精品九九久久久精品| 麻豆久久久9性大片| 久久精品国产精品亜洲毛片| 国产精品久久久久影院嫩草| 99久久国产精品免费一区二区| 国产91久久综合| 88久久精品无码一区二区毛片 | 无码任你躁久久久久久老妇| 国产综合免费精品久久久| 狠狠狠色丁香婷婷综合久久五月 | 久久永久免费人妻精品下载| 久久久精品人妻一区二区三区蜜桃| 久久强奷乱码老熟女网站| 很黄很污的网站久久mimi色|