/**
* @Author: zhangQi
* @Date: 2020-08-02 20:19
*/
public class SortTest {
public static void main(String[] args) {
/*Bubble Sort 冒泡排序 for从数组最大下标开始,逐次递减, 内层for每次两两j,j+1对比,每次两两对比
两两对比后,再从前面开始*/
int[] arr = {3, 1, 6, 2, 5};
int len = arr.length;
for (int i = len - 1; i > 0; i--) {
for (int j = 0; j < i; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
for (int ele : arr){
System.out.print(ele+" ");
}
System.out.println();
System.out.println("===================");
//选择排序
/*for从i为0开始递增,直到最大下标,内层j从i+1开始,j小于数组长度递增
每次将选定下标与之后的每一个下标的元素对比*/
int[] arr2 = {4,31,14,1,5,3};
int len2 = arr2.length;
for(int i=0;i<len2-1;i++){
for(int j=i+1;j<len2;j++){
if(arr2[i]>arr2[j]){
int temp = arr2[j];
arr2[j] = arr2[i];
arr2[i] =temp;
}
}
}
for(int ele :arr2){
System.out.print(ele+" ");
}
}
}
https://www.cnblogs.com/PythonLearner/p/13424051.html 动图