对数器
用来检验自己写的方法是否正确
冒泡排序
时间复杂度O(n),空间复杂度O(1),具有稳定性
每次对比相邻两个数,小的往上浮动。

1 package day1; 2 3 import java.util.Arrays; 4 5 public class BubbleSort { 6 7 public static void bubbleSort (int arr[] ) { 8 if ( arr == null || arr.length <2) return ; 9 10 for (int i= arr.length - 1; i > 0 ; i --) { 11 for (int j = 0 ; j < i ; j ++) { 12 if (arr[j] > arr[j+1]) 13 swap(arr,j ,j+1); 14 } 15 } 16 } 17 18 19 20 public static void swap (int arr[],int i ,int j ) { 21 // int temp = arr[i]; 22 // arr[i] = arr[j]; 23 // arr[j] = temp; 24 arr[i] = arr[i] ^ arr[j]; 25 arr[j] = arr[i] ^ arr[j]; 26 arr[i] = arr[i] ^ arr[j]; 27 28 } 29 30 31 public static void print(int arr[]) { 32 if (arr == null) return ; 33 for (int i = 0 ; i< arr.length ; i++) { 34 System.out.print(arr[i]+" "); 35 } 36 System.out.println(); 37 } 38 39 40 public static int [] generateRandomArray(int maxSize ,int maxValue) { 41 int [] arr = new int [(int) ((maxSize+1) * Math.random())]; 42 for (int i = 0 ; i < arr.length ; i ++ ) { 43 arr[i] =(int) ((maxValue +1) * Math.random() )- (int) (maxValue*Math.random() ); 44 } 45 return arr; 46 } 47 48 49 50 public static void comparator (int arr []) { 51 Arrays.sort(arr); 52 } 53 54 55 public static int[] copyArray(int arr []) { 56 if (arr == null) return null; 57 int [] res = new int [arr.length]; 58 for (int i = 0 ;i <arr.length ;i ++) { 59 res[i] = arr[i]; 60 } 61 return res; 62 } 63 64 65 public static boolean isEqual(int arr [] ,int res []) { 66 67 if (arr == null || res == null ) return true; 68 if ((arr == null && res != null) || (arr != null && res == null)) return false ; 69 if (arr.length != res.length) return false; 70 for (int i =0 ; i <arr.length ; i++) { 71 if (arr[i] != res[i]) return false; 72 } 73 return true; 74 75 76 77 } 78 79 public static void main(String[] args){ 80 int maxSize =10; 81 int maxValue = 100; 82 int testTime =1000; 83 boolean suceed = true; 84 for (int i = 0; i <testTime;i ++) { 85 int [] arr2 = new int [maxSize]; 86 arr2 = generateRandomArray(maxSize,maxValue); 87 int [] arr1 = copyArray(arr2); 88 bubbleSort(arr2); 89 comparator(arr1); 90 if (!isEqual(arr2,arr1)) { 91 suceed =false; 92 print(arr1); 93 print(arr2); 94 break; 95 } 96 97 } 98 99 System.out.println(suceed ? "Fine!" : "Fucking Fucked !"); 100 101 int [] arr = new int [maxSize]; 102 arr = generateRandomArray(maxSize,maxValue); 103 print(arr); 104 bubbleSort(arr); 105 print(arr); 106 107 } 108 109 110 }
选择排序
时间复杂度O(n),空间复杂度O(1),无稳定性
每次循环选择所有数中最大的。
------------------------------------------------------------------------------------------
这里注意一下swap函数的调用

package day1; import java.util.Arrays; public class SelectSort { public static void selectSort(int arr[]) { if (arr == null || arr.length < 2 ) return ; for (int i = 0 ;i < arr.length -1 ;i ++) { int midIndex = i; for (int j = i +1 ; j < arr.length ; j++ ) { midIndex = arr[j] < arr[midIndex] ? j : midIndex; } swap(arr,i,midIndex); //print(arr); /* * if you do next way ,it's ok .but not efficient, * but you can use the function "swap" which Annotated ; * because the this solution can't promise midIndex != i * if they are equal ,it will be 0; * it happen in The current number arr[midIndex] is the smallest number,don't need to exchange * */ // for (int j = i+1 ; j < arr.length ; j ++ ) // if (arr[i] > arr[j]) { // swap(arr,i ,j); // } } } public static void swap (int arr[] ,int i ,int j ) { // arr[i] = arr[i] ^ arr[j]; // arr[j] = arr[i] ^ arr[j]; // arr[i] = arr[i] ^ arr[j]; int tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; } //for test public static int [] generateRandomArray(int maxSize ,int maxValue) { int [] arr = new int [(int)((maxSize + 1 )* Math.random())]; for (int i = 0 ;i < arr.length ;i ++) { arr[i] = (int)((maxValue+1) * Math.random()) - (int)(maxValue * Math.random()); } return arr; } public static int [] copyArray ( int arr[]) { if (arr == null ) { return null ; } int [] res = new int [arr.length]; for (int i = 0 ; i < arr.length ;i ++) { res[i] = arr[i]; } return res; } public static void print (int arr[]) { for (int i = 0 ; i < arr.length ; i++ ) { System.out.print(arr[i] + " "); } System.out.println(); } public static void comparator (int arr[]) { Arrays.sort(arr); } public static boolean isEqual (int arr[], int res []) { if ((arr == null && res != null )|| (arr != null && res == null)) return false; if (arr == null && res == null) return true; if (arr.length != res.length) return false; for (int i = 0 ;i < arr.length ; i++ ) { if (arr[i] != res[i]) return false; } return true ; } public static void main(String[] args) { int maxSize = 30; int maxValue = 100; int testTime = 1000; boolean suceed = true; for (int i = 0 ; i < testTime ; i ++) { int [] arr1 = generateRandomArray(maxSize , maxValue); int [] arr2 = copyArray(arr1); //print(arr1); //print(arr2); selectSort(arr1); comparator(arr2); if (!isEqual (arr1,arr2)) { suceed = false; //System.out.println("********************"); print(arr1); print(arr2); break; } } System.out.println(suceed? "Nice!":"Flucking Fucked !"); } }
插入排序
时间复杂度,好情况下O(N)正序,差情况下O(N2)倒序。稳定性
先把第一个元素看做是一个已经排好序的数组,后面的元素依次往里面插入。
------------------------------------------------------------------------------------------------------------------
这里要注意,插入的时候要从最后一个元素开始对比。如果小就交换,交换结束后,依然要跟前一个元素进行对比,直到数组头或者前面的元素小于当前元素。
这样不会覆盖掉前面的元素。
1 2 6 7 | 4 (4对比7,交换)
1 2 6 4 | 7 (4对比6,交换)
1 2 4 6 | 7 (4对比2,不动,移动 | ,排好序的数组元素+1)
1 2 4 6 7 |

1 package day1; 2 3 import java.util.Arrays; 4 5 public class InsertSort { 6 7 8 9 public static void insertSort(int arr[]) { 10 if (arr == null || arr.length < 2 ) return ; 11 12 for (int end = 1 ;end < arr.length ;end ++) { 13 14 /* 15 * arr[end] insert arr length = end-1 16 * if do it in start,it will overwrite element 17 * so begin at the end 18 */ 19 for (int j = end - 1; j >= 0 && arr[j] > arr[j+1]; j--) { 20 swap(arr,j,j+1); 21 } 22 23 } 24 25 } 26 27 public static void swap (int arr[] ,int i ,int j ) { 28 arr[i] = arr[i] ^ arr[j]; 29 arr[j] = arr[i] ^ arr[j]; 30 arr[i] = arr[i] ^ arr[j]; 31 32 } 33 34 35 36 //for test 37 public static int [] generateRandomArray(int maxSize ,int maxValue) { 38 39 int [] arr = new int [(int)((maxSize + 1 )* Math.random())]; 40 for (int i = 0 ;i < arr.length ;i ++) { 41 arr[i] = (int)((maxValue+1) * Math.random()) - (int)(maxValue * Math.random()); 42 } 43 44 return arr; 45 } 46 47 48 49 50 public static int [] copyArray ( int arr[]) { 51 if (arr == null ) { 52 return null ; 53 } 54 int [] res = new int [arr.length]; 55 for (int i = 0 ; i < arr.length ;i ++) { 56 res[i] = arr[i]; 57 } 58 return res; 59 60 } 61 62 63 64 public static void print (int arr[]) { 65 for (int i = 0 ; i < arr.length ; i++ ) { 66 System.out.print(arr[i] + " "); 67 } 68 System.out.println(); 69 70 } 71 72 public static void comparator (int arr[]) { 73 Arrays.sort(arr); 74 } 75 76 77 78 public static boolean isEqual (int arr[], int res []) { 79 if ((arr == null && res != null )|| (arr != null && res == null)) return false; 80 if (arr == null && res == null) return true; 81 if (arr.length != res.length) return false; 82 for (int i = 0 ;i < arr.length ; i++ ) { 83 if (arr[i] != res[i]) return false; 84 } 85 return true ; 86 } 87 88 89 90 91 92 public static void main(String[] args) { 93 94 int maxSize = 10; 95 int maxValue = 100; 96 int testTime = 1000; 97 boolean suceed = true; 98 99 for (int i = 0 ; i < testTime ; i ++) { 100 101 int [] arr1 = generateRandomArray(maxSize , maxValue); 102 int [] arr2 = copyArray(arr1); 103 insertSort(arr1); 104 comparator(arr2); 105 if (!isEqual (arr1,arr2)) { 106 suceed = false; 107 //System.out.println("********************"); 108 print(arr1); 109 print(arr2); 110 break; 111 112 } 113 114 } 115 116 System.out.println(suceed? "Nice!":"Flucking Fucked !"); 117 118 119 } 120 }