1、选择排序(直接排序)
public class ArraySelectSort {
public static void main(String[] args) {
int[] arr = {11,12,79,2,5,20};
selectSort(arr); //结果: [79,20,12,11,5,2]
}
public static void selectSort(int[] arr){
for(int i=0;i<arr.length-1;i++){
for(int j=i+1;j<arr.length;j++){
if(arr[j]>arr[i]){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
}
2、冒泡排序
public class ArrayBubbleSort {
public static void main(String[] args) {
int[] arr = {11,12,79,2,5,20};
bubbleSort(arr); //结果: [2,5,11,12,20,79]
}
public static void 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 temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
}