java 选择排序法

 1 public class Test3 {
 2 
 3     /**@author shaobn
 4      * @param 选择排序:将a.length - i个元素分别和第i个元素相比较,小的话就将值调换。依次递减进行排列
 5      */
 6     public static void main(String[] args) {
 7         // TODO Auto-generated method stub
 8         int[] a=  {20,23,11,13,35,34,657};
 9         Method1(a);
10     }
11     public static void Method1(int[] a){
12         int temp;
13         int value;
14         for (int i = 0; i < a.length; i++) {
15             temp = i;
16             for (int j = i+1; j < a.length; j++) {
17                 if(a[j]<a[temp]){
18                     temp = j;
19                 }
20             }
21             value = a[i];
22             a[i] = a[temp];
23             a[temp] = value;
24             
25         }
26         for (int i = 0; i < a.length; i++) {
27             System.out.println(a[i]);
28         }
29     }
30 }

 

posted @ 2015-09-24 13:50  邻家小书童  阅读(154)  评论(0编辑  收藏  举报