//完成后取出数值 for(int y=0;y<length;y++){ int len = 0; for(int x=0;x<max;x++){ if(bead[x][y]!=0){ len ++; } } array[y] = len; }
} publicstaticvoidmain(String[] args){ int[] a = newint[100]; Random r = new Random(); for (int i = 0; i < a.length; i++) { a[i] = r.nextInt(10000); } System.out.println("BeadSort排序前:"); System.out.println(Arrays.toString(a)); beadSort(a); System.out.println("BeadSort排序后:"); System.out.println(Arrays.toString(a)); } }
其他注意事项
可以看到我们构建了一个 max * length 的二维数组,实际我们想下,也可以构建一个 (max-min+1) * length 的二维数组,最后排好后都加上min,理论上可以节约空间。
publicclassBogoSort{ publicstaticvoidbogoSort(int[] array){ int size = array.length; int i, j; boolean tag; while (true) { tag = true; //检测是否有序 for (i = 1; i < size; i++) { if (array[i] < array[i - 1]) { tag = false; break; } } //如果有序,则排序完成 if (tag) { break; } //顺序不对,则随机打乱 Random random = new Random(); for (i = 0; i < size; i++) { j = random.nextInt(size); //随机交换两值 swap(array, i, j); } } } privatestaticvoidswap(int[] a, int p, int q){ int temp = a[p]; a[p] = a[q]; a[q] = temp; } publicstaticvoidmain(String[] args){ int[] a = newint[10]; Random r = new Random(); for (int i = 0; i < a.length; i++) { a[i] = r.nextInt(10000); } System.out.println("BogoSort排序前:"); System.out.println(Arrays.toString(a)); bogoSort(a); System.out.println("BogoSort排序后:"); System.out.println(Arrays.toString(a)); } }
publicclassPermutationSort{ publicstaticint[] permutationSort(int[] a) { List<int[]> list = new ArrayList<>(); permute(a, a.length, list); for (int[] x : list) { if (isSorted(x)) { return x; } } return a; } //获取数组的全排列 privatestaticvoidpermute(int[] a, int n, List<int[]> list){ if (n == 1) { int[] b = newint[a.length]; System.arraycopy(a, 0, b, 0, a.length); list.add(b); return; } for (int i = 0; i < n; i++) { swap(a, i, n - 1); permute(a, n - 1, list); swap(a, i, n - 1); } } //判断数组是否有序 privatestaticbooleanisSorted(int[] a){ for (int i = 1; i < a.length; i++) { if (a[i - 1] > a[i]) { returnfalse; } } returntrue; } //交换数组两数数值 privatestaticvoidswap(int[] arr, int i, int j){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; }
publicstaticvoidmain(String[] args){ int[] a = newint[10]; Random r = new Random(); for (int i = 0; i < a.length; i++) { a[i] = r.nextInt(10000); } System.out.println("PermutationSort排序前:"); System.out.println(Arrays.toString(a)); int [] s = permutationSort(a); System.out.println("PermutationSort排序后:"); System.out.println(Arrays.toString(s)); } }
publicclassCombSort{ publicstaticvoidcombSort(int[] data){ int n = data.length; finaldouble shrink = 1.3; int i, delta = n, noswap = 0; while (noswap == 0) { for (noswap = 1, i = 0; i + delta < n; i++) { if (data[i] > data[i + delta]) { data[i] ^= data[i + delta]; data[i + delta] ^= data[i]; data[i] ^= data[i + delta]; noswap = 0; } } if (delta > 1) { delta /= shrink; noswap = 0; } } }
publicstaticvoidmain(String[] args){ int[] a = newint[100]; Random r = new Random(); for (int i=0;i<a.length;i++){ a[i] = r.nextInt(10000); } System.out.println("CombSort排序前:"); System.out.println(Arrays.toString(a)); combSort(a); System.out.println("CombSort排序后:"); System.out.println(Arrays.toString(a)); } }
publicclassCocktailSort{ publicstaticvoidcocktailSort(int[] src){ for (int i = 0; i < src.length / 2; i++) { //将最小值排到队首 for (int j = i; j < src.length - i - 1; j++) { if (src[j] > src[j + 1]) { int temp = src[j]; src[j] = src[j + 1]; src[j + 1] = temp; } } //将最大值排到队尾 for (int j = src.length - 1 - (i + 1); j > i; j--) { if (src[j] < src[j - 1]) { int temp = src[j]; src[j] = src[j - 1]; src[j - 1] = temp; } } } }
publicstaticvoidmain(String[] args){ int[] a = newint[100]; Random r = new Random(); for (int i=0;i<a.length;i++){ a[i] = r.nextInt(10000); } System.out.println("CocktailSort排序前:"); System.out.println(Arrays.toString(a)); cocktailSort(a); System.out.println("CocktailSort排序后:"); System.out.println(Arrays.toString(a)); } }
publicclassBinaryInsertionSort{ publicstaticint[] binaryInsertionSort(int[] data) { for (int i = 1, len = data.length; i < len; i++) { // 要插入的元素 int temp = data[i]; int low = 0; int high = i - 1; // 折半比较,直到找到low大于high时(找到比他大的值的位置low) while(low <= high) { int mid = (low+high)/2; if (data[mid] > temp) { high = mid - 1; } else { low = mid + 1; } } // 移动 比他大的值,全部后移 for (int j = i; j > low; j--) { data[j] = data[j-1]; } // 插入 data[low] = temp; } return data; }
publicstaticvoidmain(String[] args){ int[] a = newint[100]; Random r = new Random(); for (int i = 0; i < a.length; i++) { a[i] = r.nextInt(10000); } System.out.println("BinaryInsertionSort排序前:"); System.out.println(Arrays.toString(a)); int[] s = binaryInsertionSort(a); System.out.println("BinaryInsertionSort排序后:"); System.out.println(Arrays.toString(s)); } }
publicclassCycleSort{ publicstaticvoidcycleSort(int[] array){ for (int cs = 0, seeker, pos; cs < array.length - 1; cs++) { //假设array[cs]中的元素不合适 seeker = array[cs]; pos = cs; //找到seeker的正确位置(pos) for (int i = cs + 1; i < array.length; i++) { if (array[i] < seeker) { pos++; } } //如果seeker已经在正确的位置,继续 if (pos == cs) { continue; } //复制后移动索引pos(如果有的话) while (seeker == array[pos]) { pos++; } //seeker放到了它正确的位置(索引pos处),同时原来pos处的元素成为了新的seeker,需要找到另一个位置 seeker = set(array, seeker, pos);
//在进入下一个循环之前完成当前循环。在当前周期结束时,pos==cs,因为一个周期总是在它开始的地方结束。 while (pos != cs) { //代码同上 pos = cs; for (int i = cs + 1; i < array.length; i++) { if (array[i] < seeker) { pos++; } } while (seeker == array[pos]) { pos++; } seeker = set(array, seeker, pos); } } }
privatestaticintset(int[] array, int data, int ndx){ try { return array[ndx]; } finally { array[ndx] = data; } }
publicstaticvoidmain(String[] args){ int[] a = newint[100]; Random r = new Random(); for (int i = 0; i < a.length; i++) { a[i] = r.nextInt(10000); } System.out.println("CycleSort排序前:"); System.out.println(Arrays.toString(a)); cycleSort(a); System.out.println("CycleSort排序后:"); System.out.println(Arrays.toString(a)); } }