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

            My Links

            Blog Stats

            常用鏈接

            留言簿(1)

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            久久精品国产清高在天天线| 久久精品中文字幕一区| 无码久久精品国产亚洲Av影片 | 国产成人无码精品久久久免费| 亚洲欧美精品伊人久久| 久久久久无码精品| 欧美午夜精品久久久久免费视| 精品久久久久久中文字幕| 色悠久久久久久久综合网| 亚洲AV日韩AV永久无码久久| 久久精品不卡| 国产成人久久激情91| 久久99这里只有精品国产| 99久久国产亚洲高清观看2024| 性欧美大战久久久久久久| 一本伊大人香蕉久久网手机| 中文字幕乱码人妻无码久久| 久久精品人妻一区二区三区| 精品久久人妻av中文字幕| 久久精品国产亚洲AV香蕉| 久久久久无码精品| 国产成人久久精品二区三区| 国产精品九九九久久九九| 久久天堂AV综合合色蜜桃网 | 久久国产精品99国产精| 人妻无码αv中文字幕久久琪琪布 人妻无码精品久久亚瑟影视 | 日本五月天婷久久网站| 久久国产一片免费观看| 久久福利青草精品资源站免费| 欧美黑人又粗又大久久久| 久久SE精品一区二区| 思思久久99热只有频精品66| 日本久久久久久久久久| 四虎亚洲国产成人久久精品| 久久无码国产| 久久婷婷五月综合国产尤物app| 久久久亚洲精品蜜桃臀| 久久久久18| 综合久久久久久中文字幕亚洲国产国产综合一区首| 国产韩国精品一区二区三区久久| 久久久久久久人妻无码中文字幕爆|