选择排序:
public static void main(String[] args) {
/*
* int[] arr = { 35, 22, 34, 15, 31, 12 };
* 选择排序:(假如从小到大排序)
* 1.从索引为0的位置依次和后面的数作比较,最终小的数出现在了0索引的位置
* 比较的次数(n-1)
* 2.从索引为1的位置依次重复1的比较
* 比较的次数(n-2) ...
*/
int[] arr = {13,35, 22, 34, 15, 31, 12 };
choiceSort(arr);
}
public static void choiceSort(int[] arr) {
System.out.print("[");
int temp;
for (int j = 0; j < arr.length - 1; j++) {
for (int i = j; i < arr.length - 1; i++) {
if (arr[j] > arr[i + 1]) {
temp = arr[j];
arr[j] = arr[i + 1];
arr[i + 1] = temp;
}
}
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + ",");
}
System.out.println("]");
}
/*
* int[] arr = { 35, 22, 34, 15, 31, 12 };
* 选择排序:(假如从小到大排序)
* 1.从索引为0的位置依次和后面的数作比较,最终小的数出现在了0索引的位置
* 比较的次数(n-1)
* 2.从索引为1的位置依次重复1的比较
* 比较的次数(n-2) ...
*/
int[] arr = {13,35, 22, 34, 15, 31, 12 };
choiceSort(arr);
}
public static void choiceSort(int[] arr) {
System.out.print("[");
int temp;
for (int j = 0; j < arr.length - 1; j++) {
for (int i = j; i < arr.length - 1; i++) {
if (arr[j] > arr[i + 1]) {
temp = arr[j];
arr[j] = arr[i + 1];
arr[i + 1] = temp;
}
}
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + ",");
}
System.out.println("]");
}