1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
| public static void selectSort(int[] a){ int N = a.length; for (int i = 0; i < N; i++) { int min = i; for (int j = i+1; j < N; j++) { if(a[j]<a[min]) min=j; int t =a[i]; a[i] = a[j]; a[j] = t; } } } public static void insertSort(int[] a){ int N = a.length; for (int i = 0; i < N; i++) { int temp = a[i]; int j = i; for (; j > 0 && a[j] > temp; j--) { a[j] = a[j-1]; } a[j] = temp; } } public static void shellSort(int[] a){ for (int step = a.length/2; step > 0; step/=2) { for (int i = step; i < a.length; i++) { int temp = a[i]; int j = i; for (; j >= step && a[j-step] > temp ; j-=step) { a[j] = a[j-step]; } a[j] = temp; } } }
public void mergeSort(int[] arr,int left,int right){ if(left<right){ int mid = (left + right) / 2; mergeSort(arr,left,mid); mergeSort(arr,mid+1,right); merge(arr,left,mid,right); } }
public void merge(int[] arr, int left,int mid,int right) { int i = left; int j = mid +1; int[] temp = new int[right-left+1]; int t = 0;
while (i<=mid && j<=right){ if(arr[i]<=arr[j])temp[t++] = arr[i++]; else temp[t++] = arr[j++]; } while (i<=mid) temp[t++] = arr[i++]; while (j<=right) temp[t++] = arr[j++];
t = 0; while (left<=right) arr[left++] = temp[t++]; } public void bubbleSort(int[] arr){ for (int i = arr.length-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; } } } }
public void radixSort(int[] arr){ int max = 0; for (int i : arr) { if(i>max) max = i; } int count = (max+"").length();
for (int i = 1; i <= count; i++) {
int[][] bucket = new int[10][arr.length]; int[] bucketCount = new int[10]; for (int value : arr) { bucket[value % (10 * i)][bucketCount[value % (10 * i)]++] = value; }
int k = 0; for (int j = 0; j < 10; j++) { if(bucketCount[j]!=0) { while (bucketCount[j]!=0) arr[k++] = bucket[j][--bucketCount[j]]; } } } } public void heapSort(int[] arr){ for(int k = arr.length/2 - 1;k>=0;k--){ adjustHeap(arr,k,arr.length); }
int temp =0; for (int i = arr.length-1; i >= 0; i--) { temp =arr [0]; arr[0] = arr[i]; arr[i] = temp; adjustHeap(arr,0,i); } }
public void adjustHeap(int[] arr,int i,int length){ int temp = arr[i]; for(int k = 2*i+1;k<length;k=2*k+1){ if(k+1<length && arr[k+1]>arr[k]){ k++; } if(arr[k]>temp){ arr[i] = arr[k]; i =k; }else { break; } } arr[i] = temp; }
public void quickSort(int[] arr,int l,int r){ if(l<r){ int pivotpos = partition(arr,l,r); quickSort(arr,l,pivotpos); quickSort(arr,pivotpos+1,r); } }
public int partition(int[] arr,int l,int r){ int pivot = arr[r]; while (l<r){ while (l<r && arr[l]<pivot) l++; arr[r] = arr[l]; while (l<r && arr[r]>=pivot) r--; arr[l] = arr[r]; } arr[l] = pivot; return l; }
|
能否参与评论,且看个人手段。