publicclassIntroSort{ /** * 数据量的分界线,决定了使用quick sort/heap sort还是insertion sort */ privatestaticfinalint THRESHOLD = 16; /** * 堆排序用到的辅助函数 * @param i * @return */ privatestaticintparent(int i){ return (i - 1) / 2; } privatestaticintleft(int i){ return2 * i + 1; } privatestaticintright(int i){ return (2 * i + 2); } privatestaticvoidswap(int[] array, int index1, int index2){ int temp; temp = array[index1]; array[index1] = array[index2]; array[index2] = temp; } privatestaticvoidheapShiftDown(int[] heap, int i, int begin, int end){ int l = left(i - begin) + begin; int r = right(i - begin) + begin; int largest = i; //找出左右字节点与父节点中的最大者 if (l < end && heap[l] > heap[largest]) { largest = l; } if (r < end && heap[r] > heap[largest]) { largest = r; } //若最大者不为父节点,则需交换数据,并持续向下滚动至满足最大堆特性 if (largest != i) { swap(heap, largest, i); heapShiftDown(heap, largest, begin, end); } } /** * 自底向上的开始建堆,即从堆的倒数第二层开始 * @param heap * @param begin * @param end */ privatestaticvoidbuildHeap(int[] heap, int begin, int end){ for (int i = (begin + end) / 2; i >= begin; i--) { heapShiftDown(heap, i, begin, end); } } /** * 堆排序 * @param heap * @param begin * @param end */ privatestaticvoidheapSort(int[] heap, int begin, int end){ buildHeap(heap, begin, end); for (int i = end; i > begin; i--) { swap(heap, begin, i); heapShiftDown(heap, begin, begin, i); } } /** * 插入排序 * @param array * @param len */ privatestaticvoidinsertionSort(int[] array, int len){ int i, j, temp; for (i = 1; i < len; i++) { //store the original sorted array in temp temp = array[i]; //compare the new array with temp(maybe -1?) for (j = i; j > 0 && temp < array[j - 1]; j--) { //all larger elements are moved one pot to the right array[j] = array[j - 1]; } array[j] = temp; } } /** * 三点中值计算 * @param array * @param first * @param median * @param end * @return */ privatestaticintmedian3(int[] array, int first, int median, int end){ if (array[first] < array[median]) { return helpMethod(array, first, median, end); } else { return helpMethod(array, median, first, end); } } privatestaticinthelpMethod(int[] array, int first, int median, int end){ if (array[median] < array[end]) { return median; } elseif (array[first] < array[end]) { return end; } else { return first; } } /** * 对数组分割 * @param array * @param left * @param right * @param p * @return */ privatestaticintpartition(int[] array, int left, int right, int p){ //选择最右侧的元素作为分割标准 int index = left; swap(array, p, right); int pivot = array[right]; //将所有小于标准的点移动到index的左侧 for (int i = left; i < right; i++) { if (array[i] < pivot) { swap(array, index++, i); } } //将标准与index指向的元素交换,返回index,即分割位置 swap(array, right, index); return index; } /** * 递归的对数组进行分割排序 * @param array * @param begin * @param end * @param depthLimit */ privatestaticvoidintroSortLoop(int[] array, int begin, int end, int depthLimit){ //子数组数据量大小,则交给后面的插入排序进行处理 while ((end - begin + 1) > THRESHOLD) { //递归深度过大,则由堆排序代替 if (depthLimit == 0) { heapSort(array, begin, end); return; } --depthLimit; //使用quick sort进行排序 int cut = partition(array, begin, end, median3(array, begin, begin + (end - begin) / 2, end)); introSortLoop(array, cut, end, depthLimit); //对左半段进行递归的sort end = cut; } } /** * 计算最大容忍的递归深度 * @param n * @return */ privatestaticintlg(int n){ int k; for (k = 0; n > 1; n >>= 1) { ++k; } return k; } /** * IntroSort排序 * @param array * @param len */ publicstaticvoidintroSort(int[] array, int len){ if (len != 1) { introSortLoop(array, 0, len - 1, lg(len) * 2); insertionSort(array, 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("IntroSort排序前:"); System.out.println(Arrays.toString(a)); introSort(a,a.length); System.out.println("IntroSort排序后:"); System.out.println(Arrays.toString(a)); } }
smoothSortFix(array, i - leonardo[levels[toplevel]], toplevel - 1, levels); smoothSortFix(array, i, toplevel, levels); } } }
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("SmoothSort排序前:"); System.out.println(Arrays.toString(a)); smoothSort(a,a.length); System.out.println("SmoothSort排序后:"); System.out.println(Arrays.toString(a)); } }
publicclassTreeSelectionSort{ publicstaticvoidtreeSelectionSort(int[] array){ // 数组长度 int len = array.length; // 对一个满二叉树,节点总数 = 叶子节点数*2-1 int nodeSize = len * 2 - 1; // 这里将用数组表示二叉树的存储结构 int[] tree = newint[nodeSize + 1]; /* 填充叶子节点 */ for (int i = len - 1, j = 0; i >= 0; i--, j++) { tree[nodeSize - j] = array[i]; } /* 填充其他节点 */ for (int i = nodeSize - len; i > 0; i--) { tree[i] = tree[i * 2] < tree[i * 2 + 1] ? tree[i * 2] : tree[i * 2 + 1]; } /* 将每次找出的最小元素移走 */ // 数组a的索引 int index = 0; // 最小值的索引 int minIndex = 0; while (index < len) { // 这是tree的根节点,也是最小元素 int min = tree[1]; // 将tree中最小的元素取到a[0]中 array[index++] = tree[1]; minIndex = nodeSize; /* 从最后的叶子节点开始,直到找到最小值的索引 */ while (tree[minIndex] != min) { minIndex--; } // 将这个最小元素置为最大 tree[minIndex] = Integer.MAX_VALUE; /* 如果这个节点还有父节点,那么就将它的兄弟节点升到父亲节点位置 */ // 根结点的索引是1 while (minIndex > 1) { // 这个节点是左节点 if (minIndex % 2 == 0) { tree[minIndex / 2] = tree[minIndex] < tree[minIndex + 1] ? tree[minIndex] : tree[minIndex + 1]; minIndex = minIndex / 2; } else {// 这个节点是右节点 tree[minIndex / 2] = tree[minIndex] < tree[minIndex - 1] ? tree[minIndex] : tree[minIndex - 1]; minIndex = minIndex / 2; } } } }
publicstaticvoidmain(String[] args){ int[] a = newint[3]; Random r = new Random(); for (int i = 0; i < a.length; i++) { a[i] = r.nextInt(10000); } System.out.println("TreeSelectionSort排序前:"); System.out.println(Arrays.toString(a)); treeSelectionSort(a); System.out.println("TreeSelectionSort排序后:"); System.out.println(Arrays.toString(a)); } }
publicclassAmericanFlagSort{ /** * 10位数的基数是10 */ privatestaticfinalint NUMBER_OF_BUCKETS = 10; publicstaticvoidamericanFlagSort(int[] unsorted){ // Max number of digits int numberOfDigits = getMaxNumberOfDigits(unsorted); int max = 1; for (int i = 0; i < numberOfDigits - 1; i++) { max *= 10; } sort(unsorted, 0, unsorted.length, max); } privatestaticvoidsort(int[] unsorted, int start, int length, int divisor){ // First pass - find counts int[] count = newint[NUMBER_OF_BUCKETS]; int[] offset = newint[NUMBER_OF_BUCKETS]; int digit = 0; for (int i = start; i < length; i++) { int d = unsorted[i]; digit = getDigit(d, divisor); count[digit]++; } offset[0] = start; for (int i = 1; i < NUMBER_OF_BUCKETS; i++) { offset[i] = count[i - 1] + offset[i - 1]; } // Second pass - move into position for (int b = 0; b < NUMBER_OF_BUCKETS; b++) { while (count[b] > 0) { int origin = offset[b]; int from = origin; int num = unsorted[from]; unsorted[from] = -1; do { digit = getDigit(num, divisor); int to = offset[digit]++; count[digit]--; int temp = unsorted[to]; unsorted[to] = num; num = temp; from = to; } while (from != origin); } } if (divisor > 1) { // Sort the buckets for (int i = 0; i < NUMBER_OF_BUCKETS; i++) { int begin = (i > 0) ? offset[i - 1] : start; int end = offset[i]; if (end - begin > 1) { sort(unsorted, begin, end, divisor / 10); } } } } /** * 获取最大值 位长度 * @param unsorted * @return */ privatestaticintgetMaxNumberOfDigits(int[] unsorted){ int max = Integer.MIN_VALUE; int temp = 0; for (int i : unsorted) { temp = (int) Math.log10(i) + 1; if (temp > max) { max = temp; } } return max; } /** * 获取该位数字 * @param integer * @param divisor * @return */ privatestaticintgetDigit(int integer, int divisor){ return (integer / divisor) % 10; } 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("AmericanFlagSort排序前:"); System.out.println(Arrays.toString(a)); americanFlagSort(a); System.out.println("AmericanFlagSort排序后:"); System.out.println(Arrays.toString(a)); } }