数组元素升序排列,选择排序和冒泡排序
选择排序
public static int[] shuzu(int[] arr){
for (int i = 0; i < arr.length; i++) { //3,8,12,22,49
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] > arr[j]) {
int k = arr[i];
arr[i] = arr[j];
arr[j] = k;
}
}
}
return arr;
}
冒泡排序
public static int[] bubbleSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
//比较交换
int tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
}
}
return arr;
}