大顶堆排序,排成 1 2 3 4 5 6 7 8
1 package Sort; 2 import java.util.Arrays; 3 4 public class MergeSort { 5 6 public static void GetLeastNumbers_Solution(int [] input) { 7 8 int length = input.length; 9 for(int i=length/2-1;i>=0;i--) 10 { 11 Adjust(input,i,length-1); 12 } 13 14 for(int j = length - 1; j > 0; j--) 15 { 16 if(input[j]<input[0]) 17 { 18 int temp = input[0]; 19 input[0] = input[j]; 20 input[j] = temp; 21 Adjust(input,0,j-1); 22 } 23 } 24 25 } 26 27 public static void Adjust(int []input,int k,int length) 28 { 29 int temp = input[k]; 30 for(int i=2*k+1;i<=length;i=2*i+1) 31 { 32 if(i<length && input[i]<input[i+1]) 33 { 34 i++; 35 } 36 if(temp>input[i]) 37 { 38 break; 39 } 40 else 41 { 42 swap(input,i,k); 43 k=i; 44 } 45 46 } 47 } 48 49 public static void swap(int []input,int a,int b) 50 { 51 int temp = input[a]; 52 input[a]=input[b]; 53 input[b] = temp; 54 } 55 56 57 public static void comparator(int[] arr) { 58 Arrays.sort(arr);//系统排序绝对正确的方法 59 } 60 61 public static int[] generateRandomArray(int maxSize, int maxValue) {//随机数发生器 62 //Math.random 产生一个double [0,1) 63 64 int[] arr = new int[(int) ((maxSize + 1) * Math.random())];//产生一个随机长度的数组 65 for (int i = 0; i < arr.length; i++) { 66 arr[i] = (int) ((maxValue + 1) * Math.random()) - (int) (maxValue * Math.random());//产生一个随机数,两个随机数减一下 67 } 68 return arr; 69 } 70 71 // for test 72 public static int[] copyArray(int[] arr) { 73 if (arr == null) { 74 return null; 75 } 76 int[] res = new int[arr.length]; 77 for (int i = 0; i < arr.length; i++) { 78 res[i] = arr[i]; 79 } 80 return res; 81 } 82 83 // for test 84 public static boolean isEqual(int[] arr1, int[] arr2) { 85 if ((arr1 == null && arr2 != null) || (arr1 != null && arr2 == null)) { 86 return false; 87 } 88 if (arr1 == null && arr2 == null) { 89 return true; 90 } 91 if (arr1.length != arr2.length) { 92 return false; 93 } 94 for (int i = 0; i < arr1.length; i++) { 95 if (arr1[i] != arr2[i]) { 96 return false; 97 } 98 } 99 return true; 100 } 101 102 // for test 103 public static void printArray(int[] arr) { 104 if (arr == null) { 105 return; 106 } 107 for (int i = 0; i < arr.length; i++) { 108 System.out.print(arr[i] + " "); 109 } 110 System.out.println(); 111 } 112 113 114 public static void main(String[] args) { 115 116 int testTime = 500000; 117 int maxSize = 10; 118 int maxValue = 100; 119 boolean succeed = true; 120 for (int i = 0; i < testTime; i++) { 121 int[] arr1 = generateRandomArray(maxSize, maxValue); 122 int[] arr2 = copyArray(arr1); 123 GetLeastNumbers_Solution(arr1); 124 comparator(arr2); 125 if (!isEqual(arr1, arr2)) { 126 succeed = false; 127 break; 128 } 129 } 130 System.out.println(succeed ? "Nice!" : "Fucking fucked!"); 131 132 int[] arr = generateRandomArray(maxSize, maxValue); 133 printArray(arr); 134 GetLeastNumbers_Solution(arr); 135 printArray(arr); 136 137 } 138 139 }
小顶堆排序,排成 9 8 7 6 5 4 3 2 1
1 package Sort; 2 3 import java.util.Arrays; 4 5 public class heapsort { 6 public static void GetLeastNumbers_Solution(int [] input) { 7 8 int length = input.length; 9 for(int i=length/2-1;i>=0;i--) 10 { 11 Adjust(input, i, length-1); 12 } 13 14 for(int j=length-1;j>=0;j--) 15 { 16 if(input[0]<input[j])// 17 // 说是交换,其实质就是把大顶堆的根元素,放到数组的最后;换句话说,就是每一次的堆调整之后,都会有一个元素到达自己的最终位置 18 { 19 int temp = input[0]; 20 input[0] = input[j]; 21 input[j] = temp; 22 Adjust(input, 0, j-1); 23 } 24 } 25 26 27 } 28 public static void Adjust(int []input,int k,int length) 29 { 30 int temp = input[k]; 31 32 for(int i=2*k+1;i<=length;i=2*i+1) 33 { 34 if(i<length&& input[i]>input[i+1]) 35 { 36 i++; 37 } 38 if(temp<input[i]) 39 { 40 break; 41 } 42 else { 43 swap(input,i,k); 44 k = i; 45 } 46 } 47 } 48 public static void swap(int []input,int a,int b) 49 { 50 int temp = input[a]; 51 input[a] = input[b]; 52 input[b]=temp; 53 } 54 55 56 public static void comparator(int[] arr) { 57 Arrays.sort(arr);//系统排序绝对正确的方法 58 } 59 60 public static int[] generateRandomArray(int maxSize, int maxValue) {//随机数发生器 61 //Math.random 产生一个double [0,1) 62 63 int[] arr = new int[(int) ((maxSize + 1) * Math.random())];//产生一个随机长度的数组 64 for (int i = 0; i < arr.length; i++) { 65 arr[i] = (int) ((maxValue + 1) * Math.random()) - (int) (maxValue * Math.random());//产生一个随机数,两个随机数减一下 66 } 67 return arr; 68 } 69 70 // for test 71 public static int[] copyArray(int[] arr) { 72 if (arr == null) { 73 return null; 74 } 75 int[] res = new int[arr.length]; 76 for (int i = 0; i < arr.length; i++) { 77 res[i] = arr[i]; 78 } 79 return res; 80 } 81 82 // for test 83 public static boolean isEqual(int[] arr1, int[] arr2) { 84 if ((arr1 == null && arr2 != null) || (arr1 != null && arr2 == null)) { 85 return false; 86 } 87 if (arr1 == null && arr2 == null) { 88 return true; 89 } 90 if (arr1.length != arr2.length) { 91 return false; 92 } 93 for (int i = 0; i < arr1.length; i++) { 94 if (arr1[i] != arr2[i]) { 95 return false; 96 } 97 } 98 return true; 99 } 100 101 // for test 102 public static void printArray(int[] arr) { 103 if (arr == null) { 104 return; 105 } 106 for (int i = 0; i < arr.length; i++) { 107 System.out.print(arr[i] + " "); 108 } 109 System.out.println(); 110 } 111 112 113 public static void main(String[] args) { 114 115 int testTime = 500000; 116 int maxSize = 10; 117 int maxValue = 100; 118 boolean succeed = true; 119 for (int i = 0; i < testTime; i++) { 120 int[] arr1 = generateRandomArray(maxSize, maxValue); 121 int[] arr2 = copyArray(arr1); 122 GetLeastNumbers_Solution(arr1); 123 comparator(arr2); 124 if (!isEqual(arr1, arr2)) { 125 succeed = false; 126 break; 127 } 128 } 129 System.out.println(succeed ? "Nice!" : "Fucking fucked!"); 130 131 int[] arr = generateRandomArray(maxSize, maxValue); 132 printArray(arr); 133 GetLeastNumbers_Solution(arr); 134 printArray(arr); 135 136 } 137 138 }