排序演算法 C++代碼實現

來源:互聯網
上載者:User

標籤:mil   ack   代碼實現   冒泡排序   return   stream   for   main   data-   

插入排序:

就像摸牌,摸一張插進去,找一個哨兵。從第二個開始,和前一個比較。小的話前移一位。

#include <iostream>#include<stdlib.h>using namespace std;#define N 4   //不能加分號結束class sort{public:    void insertSort(int a[],int n);    sort()    {        cout<<"Start"<<endl;    }    sort(const sort&s);private:};void sort::insertSort(int a[],int n){    int i=1,j=0,key;//設兩個變數值的目的是內外變化。假設僅僅有一個內外的變化會產生影響    while(i<n)    //外迴圈    {        key=a[i];//把第幾個拿出來。和前邊的對照;        j=i-1;   //前邊的那個值        while(key<a[j]&&j>=0)  //內迴圈        {            a[j+1]=a[j];   //把當前值改成前一個值            j--;          //位置前移一位        }        a[j+1]=key;      //比較成功之後。把哨兵插到當前位置,注意用的是j        i++;   //轉到下次迴圈    }    cout<<"-----------\n ";    for(int i=0;i<n;i++)    {        cout<<a[i];    }}sort::sort(const sort&s){    cout<<"news\n";}int main(){    sort s;    //sort b=s;    int a[N];    cout<<"lease input arr.";    for(int i=0;i<N;i++)    {        cin>>a[i];    }    cout<<"shuchu wei";    for(int i=0;i<N;i++)    {        cout<<a[i]<<endl;    }    s.insertSort(a,N);    return 0;};
</pre><p><span style="font-family:SimHei; font-size:24px">冒泡排序:</span></p><p><span style="font-family:SimHei"></span></p><pre name="code" class="html"><span style="font-size:14px;">#include <iostream>#include<stdlib.h>using namespace std;#define N 4   //不能加分號結束class sort{public:    void insertSort(int a[],int n);    sort()    {        cout<<"Start"<<endl;    }    sort(const sort&s);    void bubblesort(int a[],int n);private:    int c;};void sort::insertSort(int a[],int n){    int i=1,j=0,key;//設兩個變數值的目的是內外變化,假設僅僅有一個內外的變化會產生影響    while(i<n)    //外迴圈    {        key=a[i];//把第幾個拿出來。和前邊的對照。        j=i-1;   //前邊的那個值        while(key<a[j]&&j>=0)  //內迴圈        {            a[j+1]=a[j];   //把當前值改成前一個值            j--;          //位置前移一位        }        a[j+1]=key;      //比較成功之後,把哨兵插到當前位置,注意用的是j        i++;   //轉到下次迴圈    }    cout<<"-----insertsort------\n ";    for(int i=0;i<n;i++)    {        cout<<a[i];    }    cout<<endl;}void sort::bubblesort(int a[],int n){    int i=0,j=n;    for(;i<n-1;i++)//外層迴圈    {        for(;j>i;j--)//內層迴圈,從最後一個開始向上冒泡。冒泡次數逐漸減一        {            if(a[j]<a[j-1])//倒數開始,兩個數進行比較,下邊是交換過程            {                int temp=a[j-1];                a[j-1]=a[j];                a[j]=temp;            }        }    }    cout<<"-----bubblesort------\n ";    for(int i=0;i<n;i++)    {        cout<<a[i];    }    cout<<endl;}sort::sort(const sort&s){    cout<<"news\n";}int main(){    sort s;    //sort b=s;    int a[N];    cout<<"lease input arr.";    for(int i=0;i<N;i++)    {        cin>>a[i];    }    cout<<"shuchu wei";    for(int i=0;i<N;i++)    {        cout<<a[i];    }    cout<<endl;    s.insertSort(a,N);    s.bubblesort(a,N);    return 0;};</span><span style="font-size:24px;"></span>



其它演算法待續....


排序演算法 C++代碼實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.