冒泡排序
int[] arr = {5,0,8,6,9,3,2,1};
for (int i = 0; i < arr.length-1; i++) {
for (int j = 0; j < arr.length - 1; j++) {
if(arr[j] > arr[j+1]){
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
System.out.println(Arrays.toString(arr));
选择排序
int[] arr = {5,0,8,6,9,3,2,1};
for (int i = 0; i <arr.length; i++) {
int index = i;
for (int j = i+1; j < arr.length; j++) {
if(arr[index] > arr[j]){
index = j;
}
}
int temp;
temp = arr[i];
arr[i] = arr[index];
arr[index] = temp;
}
System.out.println(Arrays.toString(arr));