[1] 演算法之路 - 選擇排序

來源:互聯網
上載者:User

標籤:演算法   排序   

選擇排序 – 演算法

1. 將要排序的對象分作2部份,一個是已排序的,一個是未排序的

2.  從後端未排序部份選擇一個最小值,並放入前端已排序部份的最後一個

e.g:

排序前:70 80 31 37 10 1 48 60 33 80


[1] 80 31 37 10 7048 60 33 80 選出最小值1

[1 10] 31 37 80 7048 60 33 80 選出最小值10

[1 10 31] 37 80 7048 60 33 80 選出最小值31

[1 10 31 33] 80 7048 60 37 80 ......

[1 10 31 33 37] 7048 60 80 80 ......

[1 10 31 33 37 48]70 60 80 80 ......

[1 10 31 33 37 4860] 70 80 80 ......

[1 10 31 33 37 4860 70] 80 80 ......

[1 10 31 33 37 4860 70 80] 80 ...... 


#define SWAPER(X,Y){int t;t=X;X=Y;Y=t;}選擇排序 – 程式片段:int SelectionSort(int a[],int lens){int i,j,k;for(i=0;i<lens;i++){int minIndex = i;for(j=i+1;j<lens;j++){if(a[minIndex]>a[j]){minIndex=j;}}if(i!=minIndex) SWAPER(a[i],a[minIndex]);}return 0;}int SelectionSort2(int a[],int lens){int i,j,k;for(i=0;i<lens;i++){for(j=i+1;j<lens;j++){if(a[i]<a[j]){SWAPER(a[i],a[j]);}}}return 0;}


聯繫我們

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