• <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 @ 2009-04-30 13:57 bon 閱讀(263) | 評論 (0)編輯 收藏

                 摘要: 未完  閱讀全文
            posted @ 2008-07-31 10:51 bon 閱讀(307) | 評論 (0)編輯 收藏

                 摘要: 水題  閱讀全文
            posted @ 2008-07-28 16:48 bon 閱讀(489) | 評論 (0)編輯 收藏

                 摘要: 詳細介紹KMP算法以及POJ上的一道題目  閱讀全文
            posted @ 2008-07-25 11:45 bon 閱讀(404) | 評論 (0)編輯 收藏

                 摘要: kddcup08 (1)  閱讀全文
            posted @ 2008-07-09 09:55 bon 閱讀(723) | 評論 (0)編輯 收藏

            最近在Wei的指導下,跟顯露參加了KDDCUP08的數據挖掘比賽。在這次比賽中的數據處理、算法以及其它的收獲,會以連載的方式發出來,敬請各位關注。
            posted @ 2008-06-29 10:52 bon 閱讀(1191) | 評論 (1)編輯 收藏

                 摘要: 解題報告 rather tricky  閱讀全文
            posted @ 2008-06-09 15:29 bon 閱讀(610) | 評論 (0)編輯 收藏

                 摘要: pku 1936 解題報告+LCS的理解  閱讀全文
            posted @ 2008-05-27 15:46 bon 閱讀(791) | 評論 (0)編輯 收藏

                 摘要: 遞歸 樹的深度 左兒子右兄弟  閱讀全文
            posted @ 2008-05-24 20:34 bon 閱讀(515) | 評論 (0)編輯 收藏

                 摘要: hash表  閱讀全文
            posted @ 2008-05-24 11:34 bon 閱讀(1093) | 評論 (1)編輯 收藏

            僅列出標題
            共5頁: 1 2 3 4 5 
            Google PageRank 
Checker - Page Rank Calculator
            国产高潮久久免费观看| 成人综合久久精品色婷婷| 韩国三级大全久久网站| 久久国产精品99久久久久久老狼| 国产成人精品久久一区二区三区| 2020最新久久久视精品爱| 久久综合九色欧美综合狠狠| 97香蕉久久夜色精品国产| 久久66热人妻偷产精品9| 国产成人久久久精品二区三区| 亚洲人成网站999久久久综合| 久久人妻AV中文字幕| 99久久这里只有精品| 久久国产免费直播| 久久精品久久久久观看99水蜜桃 | 久久青青国产| 麻豆AV一区二区三区久久| 94久久国产乱子伦精品免费 | 久久国产精品99精品国产| 久久99精品国产麻豆婷婷| 精品人妻伦九区久久AAA片69| 91性高湖久久久久| 亚洲中文字幕久久精品无码喷水| 久久久久久狠狠丁香| 国产69精品久久久久9999APGF | 久久精品卫校国产小美女| 91精品国产91久久| 亚洲精品国产字幕久久不卡| 精品国产乱码久久久久久浪潮| 日韩精品久久久久久久电影蜜臀| 久久996热精品xxxx| 国内精品九九久久久精品| 97视频久久久| 久久久久国产成人精品亚洲午夜| 久久99精品久久久久久动态图 | 久久精品免费全国观看国产| 亚洲国产精品一区二区久久| 亚洲AV无码久久寂寞少妇| 久久久久久一区国产精品| 久久精品国产精品青草app| 亚洲欧美日韩久久精品第一区|