Because I saw java array sorting, I wrote down several commonly used sorting methods for reference only.
(1) Sort by sort (from small to large)
int[] arr = {5,2,66,3,7};
Arrays.sort(arr);//Arrays是util包
for(int i : arr){
System.out.println(i);
}
(2) Bubble sort
From small to large
int[] arr = {5,2,66,3,7};
int temp;
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr.length-i-1;j++){
if(arr[j]>arr[j+1]){
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
for(int i:arr){
System.out.println(i);
}
From big to small
int[] arr = {5,2,66,3,7};
int temp;
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr.length-i-1;j++){
if(arr[j]<arr[j+1]){
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
for(int i:arr){
System.out.println(i);
}