顺序排序:
public static void main(String[] args) {
int[] arr = {3,8,2,5,0};
get(arr);
for (int i = 0; i < arr.length; i++) {
int j = arr[i];
System.out.println(j);
}
}
public static void get(int[] arr){
for (int i = 0; i < arr.length; i++) {
for (int j = i+1; j < arr.length; j++) {
if(arr[i] > arr[j]) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
冒泡排序:
public static void main(String[] args) {
int[] arr = {3,8,2,5,0};
get(arr);
for (int i = 0; i < arr.length; i++) {
int j = arr[i];
System.out.println(j);
}
}
public static void get(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 temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
} |