选择排序(java版)

 1 public class SelectSortTest {
 2     public static void selectSort(int[] source) {
 3         for (int i = 0; i < source.length; i++) {
 4             for (int j = i + 1; j < source.length; j++) {
 5                 if (source[i] > source[j]) {
 6                     swap(source, i, j);
 7                 }
 8             }
 9         }
10     }
11     //private 完成交换功能的子函数
12     private static void swap(int[] source, int x, int y) {
13         int temp = source[x];
14         source[x] = source[y];
15         source[y] = temp;
16     }
17     //在main中测试
18     public static void main(String[] args) {
19         int[] a = {4, 2, 1, 6, 3, 6, 0, -5, 1, 1};
20         
21         selectSort(a);
22         
23         for (int i = 0; i < a.length; i++) {
24             System.out.printf("%d ", a[i]);
25         }
26     }
27 }

 

你可能感兴趣的:(java)