选择排序--经典

 1     public static void selectSort3(Comparable[] array)
 2     {
 3         System.out.println("===========Insert Sort Started===========");
 4         Comparable temp;
 5         int min;
 6         for (int index = 0; index < array.length; index++)
 7         {
 8             // 假定第一个元素为最小元素
 9             min = index;
10             // 循环遍历元素,每遍历一个元素,与当前最小元素比较,若此元素比当前最小元素小,则将此元素置为最小元素
11             for (int time = index + 1; time < array.length; time++)
12             {
13                 if (array[time].compareTo(array[min]) < 0)
14                 {
15                     min = time;
16                 }
17             }
18             // 遍历一遍,找到一个最小元素,把此最小元素放在数组的第一个位置
19             if (min != index)
20             {
21                 temp = array[index];
22                 array[index] = array[min];
23                 array[min] = temp;
24             }
25         }
26         System.out.println("The array after sorted....");
27         System.out.println(Arrays.toString(array));
28         System.out.println("============Insert Sort Ended============");
29 
30     }

 

posted @ 2014-01-02 19:37  一路向北中  阅读(128)  评论(0)    收藏  举报