//选择排序
public SelectionSort(int[] arr){
for (int i = 0; i < arr.length - 1; i++) {
int index = arr[i];
for (int j = i + 1; j < arr.length; j++) {
if(arr[i] > arr[j]){
int temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
}
}
//冒泡排序
public BubbleSort(int[] arr){
for (int i = 0; i < arr.length ; i++) {
for(int j = 0; j < arr.length - i -1; j++){
if(arr[j] > arr[j+1]){
int temp = arr[j+1];
arr[j+1] = arr[j];
arr[j] = temp;
}
}
}
}
//插入排序
public static void sort(int[] array){
for(int i = 1; i < array.length; i++){
int j = 0;
int temp = array[i];
for(j = i - 1; j >= 0 && array[j] > temp; j--){
array[j+1] = array[j];
}
array[j+1] = temp;
}
see(array);
}
public static void see(int[] array){
for (int i = 0; i < array.length; i++) {
System.out.print(array[i]+",");
}
}
切记:外层循环控制遍历次数,内层循环控制交换次数