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

            My Links

            Blog Stats

            常用鏈接

            留言簿(1)

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            国产毛片欧美毛片久久久| 国产精品99久久久久久董美香| 久久久久亚洲精品无码网址| 久久99国产一区二区三区| 亚洲综合久久夜AV | 国产精品久久精品| 久久久WWW成人免费毛片| 无码人妻精品一区二区三区久久 | 国产日韩久久久精品影院首页| 久久国产午夜精品一区二区三区| 久久笫一福利免费导航 | 韩国三级大全久久网站| 人妻无码久久精品| 天天久久狠狠色综合| 久久精品国产亚洲αv忘忧草| 草草久久久无码国产专区| 色婷婷综合久久久久中文| 欧美成a人片免费看久久| 亚洲国产天堂久久综合网站| 麻豆AV一区二区三区久久| 色播久久人人爽人人爽人人片AV| 91久久九九无码成人网站| 久久超碰97人人做人人爱| 亚洲国产精品一区二区三区久久| 亚洲国产天堂久久综合网站| 99精品国产在热久久| 久久久久亚洲AV片无码下载蜜桃| 久久无码高潮喷水| 久久99精品国产麻豆宅宅| 思思久久99热只有频精品66| 色诱久久av| 无码人妻久久一区二区三区蜜桃| 午夜精品久久影院蜜桃 | www久久久天天com| 久久亚洲精品成人AV| 久久精品国产99久久久| 久久综合给久久狠狠97色| 成人久久久观看免费毛片| 久久精品视频网| 久久成人精品| 欧美亚洲国产精品久久|