码迷,mamicode.com
首页 > 编程语言 > 详细

选择排序

时间:2016-11-02 21:01:18      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:return   color   i++   ++   turn   sel   family   end   mil   

选择排序

 

 1 void selectsort(int a[],int n)
 2 {
 3     int i,j,k,t;
 4     for(i=0;i<n-1;i++)
 5     {
 6         k = i;
 7         for(j=i+1;j<n;j++)
 8         {
 9             if(a[j]>a[k])
10                 k = j;//只记录位置
11         }
12         if(k != i)//交换
13         {
14             t = a[i];
15             a[i] = a[k];
16             a[k] = t;
17         }
18     }
19 }
20 int main()
21 {
22     int a[10]={12,2,16,30,28,10,16,20,6,18},i;
23     cout << "原序列为:" ;
24     for(i=0;i<10;i++)
25         cout << a[i] << " ";
26     cout << endl;
27 
28     selectsort(a,10);
29     
30     cout << "排序后为:" ;
31     for(i=0;i<10;i++)
32     {
33         cout << a[i] << " ";
34     }
35     return 0;
36 }

 

选择排序

标签:return   color   i++   ++   turn   sel   family   end   mil   

原文地址:http://www.cnblogs.com/ldy-miss/p/6024125.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!