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

            My Links

            Blog Stats

            常用鏈接

            留言簿(1)

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            亚洲中文久久精品无码ww16| 久久精品夜夜夜夜夜久久| segui久久国产精品| 久久久久久亚洲精品不卡| 亚洲精品午夜国产va久久| 久久亚洲国产成人精品性色| 亚洲嫩草影院久久精品| 一本久久综合亚洲鲁鲁五月天| 久久夜色精品国产亚洲| 99久久国产综合精品网成人影院| 婷婷久久综合| 久久亚洲国产欧洲精品一| 国产免费久久精品99re丫y| 久久九九青青国产精品| 久久精品综合网| 久久99久久99小草精品免视看| 一本久久免费视频| 久久精品国产亚洲Aⅴ蜜臀色欲 | 国产精品成人99久久久久| 99久久香蕉国产线看观香| 国产69精品久久久久9999| 国产精品久久久久AV福利动漫| 无码乱码观看精品久久| 国产精品成人久久久久久久| 久久精品麻豆日日躁夜夜躁| 成人久久免费网站| 久久精品人妻中文系列| 亚洲国产香蕉人人爽成AV片久久| 精品久久久久久国产免费了| 嫩草影院久久国产精品| 国产精品99久久精品| 九九久久自然熟的香蕉图片| 亚洲AV日韩AV天堂久久| 欧美伊人久久大香线蕉综合 | a高清免费毛片久久| 无码专区久久综合久中文字幕 | 久久综合精品国产二区无码| 囯产精品久久久久久久久蜜桃 | 久久激情五月丁香伊人| 久久综合九色综合久99| 99久久99这里只有免费的精品|