数组排序

 1 public class NumSort {
 2     public static void main(String[] args) {
 3         int[] a = new int[args.length];
 4         for (int i=0; i<args.length; i++) {
 5             a[i] = Integer.parseInt(args[i]);
 6         }
 7         print(a);
 8         selectionSort(a);
 9         print(a);
10     }
11     
12     private static void selectionSort(int[] a) {
13         int k, temp;
14         for(int i=0; i<a.length; i++) {
15             k = i;
16             for(int j=k+1; j<a.length; j++) {
17                 if(a[j] < a[k]) {
18                     k = j;
19                 }
20             }
21             
22             if(k != i) {
23                 temp = a[i];
24                 a[i] = a[k];
25                 a[k] = temp;
26             }
27         }
28     }
29     
30     private static void print(int[] a) {
31         for(int i=0; i<a.length; i++) {
32             System.out.print(a[i] + " ");
33         }
34         System.out.println();
35     }
36 }

 

posted @ 2013-04-08 15:13  glt66  阅读(136)  评论(0编辑  收藏  举报