• <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)論排行榜

            久久久久国产一区二区| 中文字幕久久精品| 精品久久综合1区2区3区激情| 91精品国产色综久久| 欧美午夜A∨大片久久| 久久精品国产第一区二区三区| 青青热久久综合网伊人| 久久久久久免费视频| 国产精品久久毛片完整版| 少妇熟女久久综合网色欲| 久久国产乱子精品免费女| 18禁黄久久久AAA片| 久久精品一区二区三区中文字幕| 中文精品久久久久人妻不卡| 久久97久久97精品免视看秋霞| 精品无码久久久久国产| 久久精品人人做人人爽电影 | 青春久久| 婷婷综合久久中文字幕| 国产综合久久久久| 欧美va久久久噜噜噜久久| 2021最新久久久视精品爱| 欧美一级久久久久久久大片| 国产精品综合久久第一页| 国产精品99久久精品| 69久久夜色精品国产69| 亚洲日本va中文字幕久久| 久久精品国产乱子伦| 超级97碰碰碰碰久久久久最新| 四虎影视久久久免费观看| 蜜臀久久99精品久久久久久| 久久精品国产精品亚洲下载| 精品视频久久久久| 久久天天躁狠狠躁夜夜2020 | 久久这里只精品99re66| 久久精品综合一区二区三区| 久久中文字幕无码专区| 久久无码AV中文出轨人妻| 亚洲精品无码久久久久| 精品久久8x国产免费观看| 91精品免费久久久久久久久|