1. 在此先贡献一个网站 此网站可可视化各种排序算法的动画
排序算法动画
2. 冒泡排序
public class CheckMachine {
public static void swap(int arr[], int i, int j) {
if (i == j) return;
arr[i] = arr[i] ^ arr[j];
arr[j] = arr[i] ^ arr[j];
arr[i] = arr[i] ^ arr[j];
}
public static int[] arrGen(int maxLength) {
if (maxLength < 1) return null;
int[] arr1 = new int[(int) (Math.random() * (maxLength+1))];
for (int i = 0; i < arr1.length; i++) {
arr1[i] = (int) (Math.random() * maxLength - Math.random() * maxLength);
}
return arr1;
}
public static void arrPrint(int[] arr1) {
if (arr1 == null || arr1.length == 0) return;
for (int i = 0; i < arr1.length; i++) {
System.out.print(arr1[i] + " ");
}
}
public static boolean arrEquals(int[] arr1, int[] arr2) {
if (arr1.length != arr1.length) return false;
for (int i = 0; i < arr1.length; i++) {
if (arr1[i] != arr2[i]) return false;
}
return true;
}
public static int[] arrCopy(int[] arr1) {
if (arr1 != null && arr1.length >= 0) {
int[] arr = new int[arr1.length];
for (int i = 0; i < arr1.length; i++) {
arr[i] = arr1[i];
}
return arr;
}
throw new IllegalArgumentException("数组不可以为空");
}
}
public class 冒泡排序 {
@Test
public void test() {
long maxTime = 10990000L;
int maxLength = 22;
boolean falg = true;
for (int i = 0; i < maxTime; i++) {
int[] arr1 = CheckMachine.arrGen(maxLength);
int[] arr2 = CheckMachine.arrCopy(arr1);
bubblingSort(arr1);
Arrays.sort(arr2);
if (!CheckMachine.arrEquals(arr1, arr2)) {
falg = false;
CheckMachine.arrPrint(arr1);
break;
}
}
System.out.println(falg ? "nice" : "fuck fucking");
}
private static void bubblingSort(int[] arr) {
if (arr.length <2 || arr == null) return;
for (int i = 1; i < arr.length; i++) {
for (int j = 0; j < arr.length-i ; j++) {
if ( arr[j] > arr[j + 1] )
CheckMachine.swap(arr, j, j + 1);
}
}
}
}
3. 插入排序
public class 插入排序 {
@Test
public void test() {
long maxTime = 100L;
int maxLength = 100;
boolean falg = true;
for (int i = 0; i < maxTime; i++) {
int[] arr1 = CheckMachine.arrGen(maxLength);
int[] arr2 = CheckMachine.arrCopy(arr1);
insertSort(arr1);
Arrays.sort(arr2);
if (!CheckMachine.arrEquals(arr1, arr2)) {
falg = false;
CheckMachine.arrPrint(arr1);
break;
}
}
System.out.println(falg ? "nice" : "fuck fucking");
}
private void insertSort(int[] arr) {
if (arr.length < 2 || arr == null) return;
for (int i = 1; i < arr.length; i++) {
for (int j = i; j > 0; j--) {
if (arr[j] < arr[j - 1]) {
CheckMachine.swap(arr, j, j - 1);
} else break;
}
}
}
}
4.选择排序
public class 选择排序 {
@Test
public void test1() {
int maxTime = 10000;
int maxLength = 10;
boolean falg = true;
for (int i = 0; i < maxTime; i++) {
int[] arr1 = CheckMachine.arrGen(maxLength);
int[] arr2 = CheckMachine.arrCopy(arr1);
selectSort(arr1);
Arrays.sort(arr2);
if (!CheckMachine.arrEquals(arr1, arr2)) {
falg = false;
CheckMachine.arrPrint(arr1);
break;
}
}
System.out.println(falg ? "nice" : "fuck fucking");
}
private static void selectSort(int[] arrs) {
if (arrs.length < 2 || arrs == null) {
return;
}
for (int i = 0; i < arrs.length; i++) {
int minIndex = i;
for (int j = i + 1; j < arrs.length; j++) {
minIndex = arrs[j] < arrs[minIndex] ? j : minIndex;
}
CheckMachine.swap(arrs, minIndex, i);
}
}
}