直接插入排序

來源:互聯網
上載者:User

標籤:for   col   插入   int   pre   span   排序   temp   別人   

//直接插入排序 1 void Insertsort1(int a[],int n){    int i,j;    int tmp;    for(i=1;i<n;i++)    {        if(a[i] < a[i-1])   //如果當前所要插入的數比已排序好的最大的數大                            //則不需要再進行比較,直接放到當前位置        {         tmp = a[i];         for(j=i-1;j>=0;j--)         {           if(a[j] > tmp)                    a[j+1] = a[j];  //將比tmp的值往後排           else break;          }         a[j+1] = tmp;     //找到合適的位置        }     }}//別人部落格上寫得更加精簡void Insertsort2(int a[], int n){    int i, j;    for (i = 1; i < n; i++)        if (a[i] < a[i - 1])        {            int temp = a[i];            for (j = i - 1; j >= 0 && a[j] > temp; j--)                a[j + 1] = a[j];            a[j + 1] = temp;        }}//直接插入排序2   交換順序,而非將比tmp的值往後放void Insertsort3(int a[],int n){    int i,j;    for(i=1;i<n;i++)    {        if(a[i] < a[i-1])   //如果當前所要插入的數比已排序好的最大的數大                            //則不需要再進行比較,直接放到當前位置        {          for(j=i-1;j>=0;j--)          {           if(a[j] > a[j+1])                   Swap(a[j],a[j+1]);           else break;           }        }     }}

 

直接插入排序

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.