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

            C++ Jounior

            once setback,once inspiration,once self-awareness
            重要的是這個(gè)磨練過程,而不是結(jié)果,要的是你粗壯的腿,而不是你身上背的那袋鹽巴

             

            排序

            // 一、冒泡排序(Bubble)???



            namespace ?BubbleSorter
            {
            ????
            public ? class ?BubbleSorter
            ????
            {
            ????????
            public ? void ?Sort( int []?list)
            ????????
            {
            ????????????
            int ?i,?j,?temp;
            ????????????
            bool ?done? = ? false ;
            ????????????j?
            = ? 1 ;
            ????????????
            while ?((j? < ?list.Length)? && ?( ! done))
            ????????????
            {
            ????????????????done?
            = ? true ;
            ????????????????
            for ?(i? = ? 0 ;?i? < ?list.Length? - ?j;?i ++ )
            ????????????????
            {
            ????????????????????
            if ?(list[i]? > ?list[i? + ? 1 ])
            ????????????????????
            {
            ????????????????????????done?
            = ? false ;
            ????????????????????????temp?
            = ?list[i];
            ????????????????????????list[i]?
            = ?list[i? + ? 1 ];
            ????????????????????????list[i?
            + ? 1 ]? = ?temp;
            ????????????????????}

            ????????????????}

            ????????????????j
            ++ ;
            ????????????}

            ????????}

            ????}


            ????
            public ? class ?MainClass
            ????
            {
            ????????
            public ? static ? void ?Main1()
            ????????
            {
            ????????????
            int []?iArrary? = ? new ? int []? {? 1 ,? 5 ,? 13 ,? 6 ,? 10 ,? 55 ,? 99 ,? 2 ,? 87 ,? 12 ,? 34 ,? 75 ,? 33 ,? 47 ?} ;
            ????????????BubbleSorter?sh?
            = ? new ?BubbleSorter();
            ????????????sh.Sort(iArrary);
            ????????????
            for ?( int ?m? = ? 0 ;?m? < ?iArrary.Length;?m ++ )
            ????????????????Console.Write(
            " {0}? " ,?iArrary[m]);
            ????????????Console.WriteLine();
            ????????}

            ????}

            }
            ???
            ??
            // 二、選擇排序(Selection)???



            namespace ?SelectionSorter
            {
            ????
            public ? class ?SelectionSorter
            ????
            {
            ????????
            private ? int ?min;
            ????????
            public ? void ?Sort( int []?list)
            ????????
            {
            ????????????
            for ?( int ?i? = ? 0 ;?i? < ?list.Length? - ? 1 ;?i ++ )
            ????????????
            {
            ????????????????min?
            = ?i;
            ????????????????
            for ?( int ?j? = ?i? + ? 1 ;?j? < ?list.Length;?j ++ )
            ????????????????
            {
            ????????????????????
            if ?(list[j]? < ?list[min])
            ????????????????????????min?
            = ?j;
            ????????????????}

            ????????????????
            int ?t? = ?list[min];
            ????????????????list[min]?
            = ?list[i];
            ????????????????list[i]?
            = ?t;
            ????????????}

            ????????}

            ????}


            ????
            public ? class ?MainClass2
            ????
            {
            ????????
            public ? static ? void ?Main2()
            ????????
            {
            ????????????
            int []?iArrary? = ? new ? int []? {? 1 ,? 5 ,? 3 ,? 6 ,? 10 ,? 55 ,? 9 ,? 2 ,? 87 ,? 12 ,? 34 ,? 75 ,? 33 ,? 47 ?} ;
            ????????????SelectionSorter?ss?
            = ? new ?SelectionSorter();
            ????????????ss.Sort(iArrary);
            ????????????
            for ?( int ?m? = ? 0 ;?m? < ?iArrary.Length;?m ++ )
            ????????????????Console.Write(
            " {0}? " ,?iArrary[m]);
            ????????????Console.WriteLine();
            ????????}

            ????}

            }
            ???
            ??
            // 三、插入排序(InsertionSorter)???



            namespace ?InsertionSorter
            {
            ????
            public ? class ?InsertionSorter
            ????
            {
            ????????
            public ? void ?Sort( int []?list)
            ????????
            {
            ???????????
            ????????????
            for ?( int ?i? = ? 1 ;?i? < ?list.Length;?i ++ )
            ????????????
            {
            ????????????????
            int ?t? = ?list[i];
            ????????????????
            int ?j? = ?i;
            ????????????????
            // 依次往前推。
            ????????????????
            // 先是前兩個(gè)元素。
            ????????????????
            // 然后是前三個(gè)元素。
            ????????????????
            // 然后是前N個(gè)元素。
            ???????????????? while ?((j? > ? 0 )? && ?(list[j? - ? 1 ]? > ?t))
            ????????????????
            {
            ????????????????????list[j]?
            = ?list[j? - ? 1 ];
            ????????????????????
            -- j;
            ????????????????}

            ????????????????list[j]?
            = ?t;
            ????????????}

            ????????}

            ????}


            ????
            public ? class ?MainClass3
            ????
            {
            ????????
            public ? static ? void ?Main3()
            ????????
            {
            ????????????
            int []?iArrary? = ? new ? int []? {? 1 ,? 13 ,? 3 ,? 6 ,? 10 ,? 55 ,? 98 ,? 2 ,? 87 ,? 12 ,? 34 ,? 75 ,? 33 ,? 47 ?} ;
            ????????????InsertionSorter?ii?
            = ? new ?InsertionSorter();
            ????????????ii.Sort(iArrary);
            ????????????
            for ?( int ?m? = ? 0 ;?m? < ?iArrary.Length;?m ++ )
            ????????????????Console.WriteLine(
            " {0} " ,?iArrary[m]);
            ????????????Console.WriteLine();
            ????????}

            ????}

            }
            ???
            ??
            /*
            ???*?有一個(gè)已經(jīng)有序的數(shù)據(jù)序列,要求在這個(gè)已經(jīng)排好的數(shù)據(jù)序列中插入一個(gè)數(shù),但要求插入后此數(shù)據(jù)序列仍然有序,這個(gè)時(shí)候就要用到一種新的排序方法——插入排序法,插入排序的基本操作就是將一個(gè)數(shù)據(jù)插入到已經(jīng)排好序的有序數(shù)據(jù)中,從而得到一個(gè)新的、個(gè)數(shù)加一的有序數(shù)據(jù)。

            void??insertSort(Type*?arr,long?len)?//InsertSort?algorithm
            {
            ???????long?i=0,j=0;//iterator?value
            ???????Type?tmpData;
            ???????assertF(arr!=NULL,"In?InsertSort?sort,arr?is?NULL\n");
            ???????for(i=1;i<len;i++)
            ???????{
            ??????????????j=i;
            ??????????????tmpData=arr;
            ??????????????while(tmpData<arr[j-1]&&j>0)
            ??????????????{
            ?????????????????????arr[j]=arr[j-1];
            ?????????????????????j--;
            ??????????????}
            ??????????????arr[j]=tmpData;
            ???????}
            }
            插入排序算法思路是:
            假定這個(gè)數(shù)組的序是排好的,然后從頭往后,如果有數(shù)比當(dāng)前外層元素的值大,則將這個(gè)數(shù)的位置往后挪,直到當(dāng)前外層元素的值大于或等于它前面的位置為止.這具算法在排完前k個(gè)數(shù)之后,可以保證a[1…k]是局部有序的,保證了插入過程的正確性.

            ???
            */

            // 四、希爾排序(ShellSorter)???



            namespace ?ShellSorter
            {
            ????
            public ? class ?ShellSorter
            ????
            {
            ????????
            public ? void ?Sort( int []?list)
            ????????
            {
            ????????????
            int ?inc;
            ????????????
            for ?(inc? = ? 1 ;?inc? <= ?list.Length? / ? 9 ;?inc? = ? 3 ? * ?inc? + ? 1 )?;
            ????????????Console.WriteLine(
            " {0}--- " ,inc);

            ????????????
            for ?(;?inc? > ? 0 ;?inc? /= ? 3 )
            ????????????
            {
            ????????????????Console.WriteLine(
            " {0}?around " ,?inc);
            ????????????????
            for ?( int ?i? = ?inc? + ? 1 ;?i? <= ?list.Length;?i? += ?inc)
            ????????????????
            {
            ????????????????????
            // inc?是間隔
            ????????????????????
            // j?是每幾個(gè)元素,1為開始坐標(biāo)
            ???????????????????? int ?t? = ?list[i? - ? 1 ]; // “被比較的數(shù)”的元素放到?t?中
            ???????????????????? int ?j? = ?i;????????????????????
            ????????????????????
            while ?((j? > ?inc)? && ?(list[j? - ?inc? - ? 1 ]? > ?t))
            ????????????????????
            { // 因?yàn)橐容^的數(shù)(前面的數(shù))大于“被比較的數(shù)”
            ????????????????????????list[j? - ? 1 ]? = ?list[j? - ?inc? - ? 1 ];
            ????????????????????????j?
            -= ?inc; // 移動(dòng)比較元素
            ????????????????????}

            ????????????????????list[j?
            - ? 1 ]? = ?t; // 最后把?t?放在比較范圍的每個(gè)位置
            ????????????????}

            ????????????}

            ????????}

            ????}


            ????
            public ? class ?MainClass4
            ????
            {
            ????????
            public ? static ? void ?Main4()
            ????????
            { // ??????????????????????????????0??1??2???3??4??5
            ???????????? int []?iArrary? = ? new ? int []? {? 15 ,? 5 ,? 13 ,? 6 ,? 10 ,? 55 ,? 99 ,? 2 ,? 87 ,? 12 ,? 34 ,? 75 ,? 33 ,? 47 ?} ;
            ????????????ShellSorter?sh?
            = ? new ?ShellSorter();
            ????????????sh.Sort(iArrary);
            ????????????
            for ?( int ?m? = ? 0 ;?m? < ?iArrary.Length;?m ++ )
            ????????????????Console.WriteLine(
            " {0}? " ,?iArrary[m]);
            ????????????Console.WriteLine();
            ????????}

            ????}

            }




            /* 希爾排序(縮小增量法)?
            屬于插入類排序,是將整個(gè)無(wú)序列分割成若干小的子序列分別進(jìn)行插入排序??
            排序過程:先取一個(gè)正整數(shù)d1<n,把所有序號(hào)相隔d1的數(shù)組元素放一組,
            ?*?組內(nèi)進(jìn)行直接插入排序;然后取d2<d1,重復(fù)上述分組和排序操作;
            ?*?直至di=1,即所有記錄放進(jìn)一個(gè)組中排序?yàn)橹???
            ????
            ??初始:d=5???
            ??????????49???38???65???97???76???13???27???49*???55???04???
            ???????????|----------------------------|???
            ???????????????38??????????????????????????????27???
            ???????????????????|---------------------------|???
            ????????????????????????65??????????????????????????????49*???????
            ?????????????????????????|----------------------------|???
            ????????????????????????????????97??????????????????????????????55???
            ??????????????????????????????????|--------------------------|???
            ??????????????????????????????????????76????????????????????????????????04
            ????????????????????????????????????????|-----------------------------|?
            ??一趟結(jié)果???
            ??????????13???27???49*?55???04???49???38???65?????97?????76??
            ??d=3
            ??????????13???27???49*?55???04???49???38???65?????97?????76??
            ?????????????|---------------|----------------|--------------------|???
            ??????????????????27????????????????04?????????????????65???
            ???????????????????|----------------|----------------|???
            ?????????????????????????49*???????????????49??????????????????97???
            ???????????????????????????|----------------|-----------------|???
            ??二趟結(jié)果???
            ????????????13???04???49*?38???27???49???66???65???97???76???
            ??d=1???
            ????????????13???04???49*?38???27???49???66???65???97???76???
            ??????????????|-----|-----|-----|-----|-----|-----|-----|-----|-----|
            ??三趟結(jié)果???
            ????????????04???13???27???38???49*?49???55???65???76???97????
            */

            posted on 2008-04-02 09:26 snowball 閱讀(226) 評(píng)論(0)  編輯 收藏 引用 所屬分類: 算法+數(shù)據(jù)結(jié)構(gòu)

            導(dǎo)航

            留言簿(1)

            隨筆分類

            友情鏈接

            搜索

            最新隨筆

            最新評(píng)論

            閱讀排行榜

            欧美亚洲色综久久精品国产| 精品欧美一区二区三区久久久| 久久强奷乱码老熟女网站| 国产成人综合久久综合| 久久精品中文字幕无码绿巨人| 狠狠色噜噜狠狠狠狠狠色综合久久| 精品熟女少妇a∨免费久久| 亚洲国产成人精品久久久国产成人一区二区三区综 | 久久99精品久久只有精品| 国产精品久久久久…| 亚洲精品综合久久| 日韩精品久久无码人妻中文字幕 | 精产国品久久一二三产区区别 | 久久无码人妻一区二区三区| 久久精品午夜一区二区福利| 久久精品国产亚洲av麻豆蜜芽| 国产精品一区二区久久精品涩爱| 91视频国产91久久久| 狼狼综合久久久久综合网| 国产精品久久久久蜜芽| 亚洲精品乱码久久久久久蜜桃| 亚洲AV无一区二区三区久久| 熟妇人妻久久中文字幕| 久久精品青青草原伊人| 91精品国产9l久久久久| 国产精品欧美久久久天天影视| 1000部精品久久久久久久久| 久久天天躁狠狠躁夜夜avapp| 亚洲AV乱码久久精品蜜桃| 狠狠色丁香久久综合五月| 久久99国产亚洲高清观看首页| 精品免费久久久久国产一区| 久久国产香蕉视频| 亚洲va久久久噜噜噜久久天堂| 色偷偷久久一区二区三区| 久久精品国产亚洲AV香蕉| 精品久久久久久无码国产| 漂亮人妻被中出中文字幕久久| 97久久超碰成人精品网站| 亚洲精品视频久久久| 国产精品久久久久久久|