publicclassFlashSort{ publicstaticvoidflashSort(int[] array){ //桶排序 partialFlashSort(array, array.length); //桶内元素使用插入排序 insertionSort(array); } privatestaticvoidpartialFlashSort(int[] a, int n){ //m值,取0.1n,也可以自由指定 int bucketSize = n / 10; if (bucketSize < 1) { bucketSize = 1; } //构建bucket int[] buckets = newint[bucketSize]; int i, j, k; int min = a[0]; int maxIndex = 0;
//找到最大最小值 for (i = 1; i < n; i++) { if (a[i] < min) { min = a[i]; } if (a[i] > a[maxIndex]) { maxIndex = i; } } if (min == a[maxIndex]) { return; } //计算系数 double c1 = ((double) bucketSize - 1) / (a[maxIndex] - min); //计算元素位置 for (i = 0; i < n; i++) { k = (int) (c1 * (a[i] - min)); buckets[k]++; } for (k = 1; k < bucketSize; k++) { buckets[k] += buckets[k - 1]; } //元素入桶 int hold = a[maxIndex]; a[maxIndex] = a[0]; a[0] = hold;
int nmove = 0; int flash; j = 0; k = bucketSize - 1;
while (nmove < n - 1) { while (j > (buckets[k] - 1)) { j++; k = (int) (c1 * (a[j] - min)); } flash = a[j]; while (j != buckets[k]) { k = (int) (c1 * (flash - min)); hold = a[buckets[k] - 1]; a[buckets[k] - 1] = flash; flash = hold; buckets[k]--; nmove++; } } } privatestaticvoidinsertionSort(int[] a){ int i, j, hold; for (i = a.length - 3; i >= 0; i--) { if (a[i + 1] < a[i]) { hold = a[i]; j = i; while (a[j + 1] < hold) { a[j] = a[j + 1]; j++; } a[j] = hold; } } }
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("FlashSort排序前:"); System.out.println(Arrays.toString(a)); flashSort(a); System.out.println("FlashSort排序后:"); System.out.println(Arrays.toString(a)); } }
publicclassPatienceSort{ publicstaticvoidpatienceSort(int[] theArray){ List<List<Integer>> newList = new ArrayList<>(); for (int i = 0; i < theArray.length; i++) { List<Integer> bucketList = new ArrayList<>(); //先开始创建一个堆 if (i == 0) { bucketList.add(theArray[i]); newList.add(bucketList); } else { boolean isOk = false; for (int j = 0; j < newList.size(); j++) { //如果当前元素比堆内的第一个元素小,就放入该堆头部作为新的第一个元素,然后执行下个元素判断 //这儿我们直接放到第一个符合的堆里了,其实放到其它符合的也是可以的,放到最后一个符合的堆里还可以解决子序列问题 if (theArray[i] < (int) ((List) newList.get(j)).get(0)) { (newList.get(j)).add(0, theArray[i]); isOk = true; break; } } //如果当前元素比所有堆内的第一个元素大,就创建个新堆,把元素作为第一个元素放进去 if (!isOk) { bucketList.add(theArray[i]); newList.add(bucketList); } } } ////生成的堆合并,而后使用插入排序 int q = 0; for (int m = 0; m < newList.size(); m++) { for (int n = 0; n < (newList.get(m)).size(); n++) { theArray[q] = (int) ((List) newList.get(m)).get(n); q++; } } //插入排序 int tmp; int j; for (int i = 1; i < theArray.length; i++) { tmp = theArray[i]; for (j = i - 1; j >= 0 && theArray[j] > tmp; j--) { theArray[j + 1] = theArray[j]; } theArray[j + 1] = tmp; } }
publicstaticvoidmain(String[] args){ int[] a = newint[200]; Random r = new Random(); for (int i=0;i<a.length;i++){ a[i] = r.nextInt(10000); } System.out.println("PatienceSort排序前:"); System.out.println(Arrays.toString(a)); patienceSort(a); System.out.println("PatienceSort排序后:"); System.out.println(Arrays.toString(a)); }
publicstatic <E extends Comparable<? super E>> voidpatienceSort(E[] n){ List<Pile<E>> piles = new ArrayList<Pile<E>>(); //生成堆 for (E x : n) { Pile<E> newPile = new Pile<E>(); newPile.push(x); int i = Collections.binarySearch(piles, newPile); if (i < 0){ i = ~i; } if (i != piles.size()) { piles.get(i).push(x); }else { piles.add(newPile); } } //使用优先级队列处理数据 PriorityQueue<Pile<E>> heap = new PriorityQueue<Pile<E>>(piles); for (int c = 0; c < n.length; c++) { Pile<E> smallPile = heap.poll(); n[c] = smallPile.pop(); if (!smallPile.isEmpty()) { heap.offer(smallPile); } } assert(heap.isEmpty()); } privatestaticclassPile<EextendsComparable<? superE>> extendsStack<E> implementsComparable<Pile<E>> { @Override publicintcompareTo(Pile<E> y){ return peek().compareTo(y.peek()); } } }
publicclassStoogeSort{ publicstaticvoidstoogeSort(int[] array){ stoogeSort(array, 0, array.length - 1); } privatestaticvoidstoogeSort(int[] array, int low, int high){ //如果第一个数大于最后一个数,交换位置 if (array[low] > array[high]) { swap(array, low, high); } if (low + 1 >= high){ return; } int third = (high - low + 1) / 3; //排序前2/3数组元素 stoogeSort(array, low, high - third); //排序后2/3数组元素 stoogeSort(array, low + third, high); //排序前2/3数组元素 stoogeSort(array, low, high - third); } privatestaticvoidswap(int[] a, int b, int c){ if (b == c){ return; } int temp = a[b]; a[b] = a[c]; a[c] = 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("StoogeSort排序前:"); System.out.println(Arrays.toString(a)); stoogeSort(a); System.out.println("StoogeSort排序后:"); System.out.println(Arrays.toString(a)); } }
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("PancakeSort排序前:"); System.out.println(Arrays.toString(a)); pancakeSort(a); System.out.println("PancakeSort排序后:"); System.out.println(Arrays.toString(a)); } }
publicclassInPlaceMergeSort{ publicstaticvoidinPlaceMergeSort(int[] array){ inPlaceMergeSort(array, 0, array.length - 1); } privatestaticvoidinPlaceMergeSort(int[] array, int first, int last){ int mid, lt, rt; int tmp; if (first >= last) { return; } mid = (first + last) / 2; inPlaceMergeSort(array, first, mid); inPlaceMergeSort(array, mid + 1, last); lt = first; rt = mid + 1; // One extra check: can we SKIP the merge? if (array[mid] <= array[rt]) { return; } while (lt <= mid && rt <= last) { // Select from left: no change, just advance lt if (array[lt] <= array[rt]) { lt++; // Select from right: rotate [lt..rt] and correct } else { // Will move to [lt] tmp = array[rt]; System.arraycopy(array, lt, array, lt + 1, rt - lt); array[lt] = tmp; // EVERYTHING has moved up by one lt++; mid++; rt++; } } // Whatever remains in [rt..last] is in place }
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("InPlaceMergeSort排序前:"); System.out.println(Arrays.toString(a)); inPlaceMergeSort(a); System.out.println("InPlaceMergeSort排序后:"); System.out.println(Arrays.toString(a)); } }
publicclassStrandSort{ privatestatic <E extends Comparable<? super E>> LinkedList<E> strandSort(LinkedList<E> list){ if (list.size() <= 1) { return list; } LinkedList<E> result = new LinkedList<>(); while (list.size() > 0) { LinkedList<E> sorted = new LinkedList<>(); //same as remove() or remove(0) sorted.add(list.removeFirst()); for (Iterator<E> it = list.iterator(); it.hasNext(); ) { E elem = it.next(); if (sorted.peekLast().compareTo(elem) <= 0) { //same as add(elem) or add(0, elem) sorted.addLast(elem); it.remove(); } } result = merge(sorted, result); } return result; }
privatestatic <E extends Comparable<? super E>> LinkedList<E> merge(LinkedList<E> left, LinkedList<E> right){ LinkedList<E> result = new LinkedList<>(); while (!left.isEmpty() && !right.isEmpty()) { //change the direction of this comparison to change the direction of the sort if (left.peek().compareTo(right.peek()) <= 0) { result.add(left.remove()); } else { result.add(right.remove()); } } result.addAll(left); result.addAll(right); return result; }
publicstaticvoidstrandSort(int[] array){ LinkedList<Integer> linkedList = new LinkedList<>(); Arrays.stream(array).forEach(e -> linkedList.add(e)); List<Integer> list = strandSort(linkedList); assert list.size() == array.length; for(int i=0;i<array.length;i++){ array[i] = list.get(i); } } 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("StrandSort排序前:"); System.out.println(Arrays.toString(a)); strandSort(a); System.out.println("StrandSort排序后:"); System.out.println(Arrays.toString(a)); } }