首页 > 代码库 > 选择排序

选择排序

 1 public class SlectedSort
 2 {
 3     public static void sort(int[] a)
 4     {
 5         int N = a.length;
 6         for (int i = 0; i < N; i++)
 7         {
 8             int min = i;
 9             for (int k = i+1; k < N; k++)
10             {
11                 if (a[k] < a[min])
12                     min = k;
13             }
14             
15             int temp = 0;
16             temp = a[i];
17             a[i] = a[min];
18             a[min] = temp;
19         }
20     }
21     
22     public static void main(String[] args)
23     {
24         int[] a = {1, 6, 4, 3, 5, 2};
25         SlectedSort.sort(a);
26         for (int i : a)
27             System.out.print(i + " ");
28     }
29 }