• <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 閱讀(263) 評論(0)  編輯 收藏 引用

            My Links

            Blog Stats

            常用鏈接

            留言簿(1)

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            亚洲成av人片不卡无码久久| 亚洲成色www久久网站夜月| 久久久久久久精品妇女99| 久久99九九国产免费看小说| 亚洲а∨天堂久久精品9966| 伊人久久大香线蕉av不卡| 亚洲AV无码久久精品蜜桃| 精品久久久久久中文字幕人妻最新| 久久99精品国产一区二区三区| 精品国产VA久久久久久久冰| 久久播电影网| 久久不见久久见免费视频7| 久久精品国产精品亚洲人人 | 久久亚洲国产精品123区| 热久久这里只有精品| 国产精品99久久久久久宅男小说| 天堂久久天堂AV色综合| 欧美成a人片免费看久久| 久久精品国产免费一区| 精品伊人久久大线蕉色首页| 久久久午夜精品| 欧美精品一区二区久久| 亚洲国产成人久久笫一页| 久久亚洲国产成人精品性色| 久久免费视频1| 久久水蜜桃亚洲av无码精品麻豆| 久久人人超碰精品CAOPOREN | 色综合久久综合中文综合网| 精品一二三区久久aaa片| 亚洲va久久久噜噜噜久久狠狠| 无码八A片人妻少妇久久| 久久久久亚洲AV无码专区体验 | 精品久久久久久无码中文野结衣| 久久国产欧美日韩精品| 国产成人精品久久亚洲| 久久久免费观成人影院| 精品乱码久久久久久久| 久久www免费人成精品香蕉| 色综合久久夜色精品国产| 乱亲女H秽乱长久久久| 亚洲成av人片不卡无码久久|