diff --git a/codes/tangikru/18963669.java b/codes/tangikru/18963669.java new file mode 100644 index 0000000000000000000000000000000000000000..c2557a3d4461512dfa27172c6932687072e0f753 --- /dev/null +++ b/codes/tangikru/18963669.java @@ -0,0 +1,22 @@ +/** + * 选择排序函数 + * aa bb cc + * @param a 待排序的数组 + * @param n 待排序的数组长度 + */ +public static void selectionSort(int[] a, int n) { + // 你的代码,使无序数组 a 变得有序 + for (int i = 0; i < n - 1; i++) { // 总共需要进行 n-1 轮排序 + int maxIndex = i; // 假设当前索引为最大值的索引 + for (int j = i + 1; j < n; j++) { // 从 i+1 开始,找到最大值的索引 + if (a[j] > a[maxIndex]) { + maxIndex = j; // 更新最大值的索引 + } + } + if (maxIndex != i) { // 如果最大值的索引不是当前轮次的起始索引,则交换 + int temp = a[i]; + a[i] = a[maxIndex]; + a[maxIndex] = temp; + } + } +} // end