##冒泡,选择排序
1.首先了解冒泡排序原理如图
冒泡排序源码
public class Sort {
public static void main(String[] args) {
int[] arr = {8,9,6,4,3,7,2,1};
for (int j = 0; j < arr.length-1; j++) {
for (int i = 0; i < arr.length-1; i++) {
if(arr[i]>arr[i+1]){
int t = arr[i];
arr[i]=arr[i+1];
arr[i+1]=t;
}
}
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]);
}
}
}
2.选择排序
选择排序源码
public class Sort {
public static void main(String[] args) {
int[] arr = {8,9,6,4,3,7,2,1};
for (int i = 0; i < arr.length-1; i++) {
for (int j = i+1; j <arr.length; j++) {
if(arr[i]>arr[j]){
int t = arr[i];
arr[i]=arr[j];
arr[j]=t;
}
}
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]);
}
}
}
以上两种排序,都是基础排序。简单易懂