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

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

            前面給出了插入排序,其基于插入牌的機制
            下面給出選擇排序和冒泡排序的原理和實現

            選擇排序:
            就是從后面的部分選擇最小值(或者最大值)來代替前者,核心算法為:
            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;
                }
            }

            具體代碼實現為:
            #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;
            }

            冒泡算法主要是從后面開始往上面進行冒泡,需要冒泡的話,必須要相鄰的元素之間進行比較,其實現代碼如下:
            #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 閱讀(264) 評論(0)  編輯 收藏 引用

            My Links

            Blog Stats

            常用鏈接

            留言簿(1)

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            久久久久国产精品麻豆AR影院| 国产69精品久久久久久人妻精品| 久久精品国产网红主播| 久久精品国产亚洲AV电影| 久久中文字幕一区二区| 久久综合视频网站| 国产精品一区二区久久| 青青久久精品国产免费看| 老色鬼久久亚洲AV综合| 久久男人AV资源网站| 久久精品中文字幕无码绿巨人| 99久久亚洲综合精品成人| 97精品国产97久久久久久免费| 国产AⅤ精品一区二区三区久久| 伊人久久综合无码成人网| 久久久久久久国产免费看| 97久久精品无码一区二区天美| 最新久久免费视频| 久久精品国产99久久久香蕉| 国产精品久久久久久福利69堂| 久久久久久国产精品无码下载 | 久久综合给合久久狠狠狠97色| 9999国产精品欧美久久久久久| 欧美丰满熟妇BBB久久久| 婷婷久久综合| 色偷偷91久久综合噜噜噜噜| 国产激情久久久久影院老熟女| 国产成人久久AV免费| 久久婷婷五月综合97色| 伊人久久大香线蕉综合影院首页| 久久免费大片| 久久乐国产综合亚洲精品| 久久久久久久免费视频| 亚洲人AV永久一区二区三区久久 | 亚洲国产精品久久电影欧美| 久久午夜无码鲁丝片午夜精品| 精品久久久久久久中文字幕| 久久99国产一区二区三区| 色8激情欧美成人久久综合电| 一级A毛片免费观看久久精品| 成人综合久久精品色婷婷|