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

            Problem Solving using C++

            Algorithm Study using C++

            排序算法總結(jié)(2)--選擇排序和冒泡排序

            前面給出了插入排序,其基于插入牌的機(jī)制
            下面給出選擇排序和冒泡排序的原理和實(shí)現(xiàn)

            選擇排序:
            就是從后面的部分選擇最小值(或者最大值)來代替前者,核心算法為:
            for(int i=0;i<size;i++)
            {
                 //assume the smallest value is at size-1
                  int temp = arr[size-1];
                  int index = size-1;
             
                  //compare the rest(from i--->size-1)
                 for(j=i;j<size-1;j++)
                 {
                       if(arr[j]<temp)
                      {
                            temp = arr[j];
                            index = j;
                      }
                 }

                //exchange the value
                 if(index!=i)
                {
                   arr[index]=arr[i];
                   arr[i]=temp;
                }
            }

            具體代碼實(shí)現(xiàn)為:
            #include <iostream>
            #include 
            <algorithm>
            #include 
            <iterator>

            using namespace std;

            int main(int argc,char* argv[])
            {
                
            int arr[]={5,6,1,2,7,3,8,10,4,9};
                
            int size = sizeof(arr)/sizeof(arr[0]);
                
                copy(arr,arr
            +size,ostream_iterator<int>(cout," "));
                cout
            <<endl;

                
            for(int i=0;i<size;i++)
                {
                    
            int temp = arr[size-1];
                    
            int index = size-1;
                    
                    
            for(int j=i;j<size-1;j++)
                    {
                        
            if(arr[j]<temp)
                        {
                            temp 
            = arr[j];
                            index 
            = j;
                        }
                    }
                    
                    
            if(i!=index)
                    {
                        arr[index]
            =arr[i];
                        arr[i]
            =temp;
                    }
                }
                
                copy(arr,arr
            +size,ostream_iterator<int>(cout," "));
                cout
            <<endl;
                
                system(
            "pause");

                
            return 0;
            }

            冒泡算法主要是從后面開始往上面進(jìn)行冒泡,需要冒泡的話,必須要相鄰的元素之間進(jìn)行比較,其實(shí)現(xiàn)代碼如下:
            #include <iostream>
            #include 
            <algorithm>
            #include 
            <iterator>

            using namespace std;

            int main(int argc,char* argv[])
            {
                
            int arr[]={5,6,1,2,7,3,8,10,4,9};
                
            int size = sizeof(arr)/sizeof(arr[0]);
                
                copy(arr,arr
            +size,ostream_iterator<int>(cout," "));
                cout
            <<endl;

               
            for(int i=0;i<size;i++)
                for(int j=size-1;j>i;j--)
                {
                    if(arr[j]<arr[j-1])
                    {
                        int temp = arr[j];
                        arr[j]=arr[j-1];
                        arr[j-1]=temp;
                    }
                }

                
                copy(arr,arr
            +size,ostream_iterator<int>(cout," "));
                cout
            <<endl;
                
                system(
            "pause");

                
            return 0;
            }

            posted on 2007-08-22 10:38 Kingoal Lee's Alogrithm Study using cplusplus 閱讀(269) 評(píng)論(0)  編輯 收藏 引用


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            My Links

            Blog Stats

            常用鏈接

            留言簿(1)

            隨筆檔案

            搜索

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            国色天香久久久久久久小说| jizzjizz国产精品久久| 久久免费观看视频| 久久天天躁狠狠躁夜夜av浪潮| 久久亚洲欧洲国产综合| 一本色综合网久久| 国产福利电影一区二区三区久久久久成人精品综合 | 久久久亚洲欧洲日产国码是AV| 久久精品国产亚洲AV久| 99久久精品免费看国产| 精品久久久久久国产| 国产三级精品久久| 97久久国产综合精品女不卡| 久久精品国产福利国产秒| 少妇无套内谢久久久久| 激情五月综合综合久久69| 久久综合给合久久狠狠狠97色69| 精品熟女少妇aⅴ免费久久| 久久婷婷五月综合国产尤物app| 色婷婷久久综合中文久久一本| 2021久久国自产拍精品| 亚洲国产一成人久久精品| 久久久精品波多野结衣| 久久福利青草精品资源站免费| 亚洲精品无码久久千人斩| 久久久国产亚洲精品| 久久久免费观成人影院| 国产精品久久久久久影院| 久久水蜜桃亚洲av无码精品麻豆| 日日狠狠久久偷偷色综合0 | 97久久超碰国产精品2021| 久久精品国产清自在天天线| 久久国产AVJUST麻豆| 中文字幕无码久久人妻| 欧美久久亚洲精品| 国产亚州精品女人久久久久久| 精品熟女少妇a∨免费久久| 久久精品国产99久久无毒不卡| 五月丁香综合激情六月久久| 一本色道久久99一综合| 久久精品夜夜夜夜夜久久|