选择排序
| 4 | * 选择排序 |
| 5 | */ |
| 6 | public class SelectionSort implements IArraySort { |
| 7 | |
| 8 | @Override |
| 9 | public int[] sort(int[] sourceArray) throws Exception { |
| 10 | int[] arr = Arrays.copyOf(sourceArray, sourceArray.length); |
| 11 | |
| 12 | // 总共要经过 N-1 轮比较 |
| 13 | for (int i = 0; i < arr.length - 1; i++) { |
| 14 | int min = i; |
| 15 | |
| 16 | // 每轮需要比较的次数 N-i |
| 17 | for (int j = i + 1; j < arr.length; j++) { |
| 18 | if (arr[j] < arr[min]) { |
| 19 | // 记录目前能找到的最小值元素的下标 |
| 20 | min = j; |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | // 将找到的最小值和i位置所在的值进行交换 |
| 25 | if (i != min) { |
| 26 | int tmp = arr[i]; |
| 27 | arr[i] = arr[min]; |
| 28 | arr[min] = tmp; |
| 29 | } |
| 30 | |
| 31 | } |
| 32 | return arr; |
| 33 | } |
| 34 | } |
nothing calls this directly
no outgoing calls
no test coverage detected