算法(Algorithms)第4版 练习 2.2.11(最终)
package com.qiusongde; import edu.princeton.cs.algs4.In; import edu.princeton.cs.algs4.StdOut; public class MergeX { private static Comparable[] aux; private final static int CUTOFF = 8;//size public static void sort(Comparable[] input) { int N = input.length; aux = input.clone();//must be clone(the same as input) StdOut.println("input:" + input + " aux:" + aux);//for test sort(aux, input, 0, N-1); } //this level takes source as input(need to be sorted) //and destination as auxiliary, and put the result in destination private static void sort(Comparable[] source, Comparable[] destination, int lo, int hi) {//avoid copy if((lo+CUTOFF-1) >= hi) { //use insertion sort for tiny subarrays insertionsort(destination, lo, hi);//prepare destination for up level return; } int mid = lo + (hi-lo)/2; sort(destination, source, lo, mid);//down level switch the roles of the input array and auxiliary array sort(destination, source, mid+1, hi); if(!less(source[mid+1], source[mid])) {//ship merge System.arraycopy(source, lo, destination, lo, hi-lo+1);//prepare destination for up level StdOut.println("destination:" + destination);//for test StdOut.printf("skip merge(source, destination, %4d, %4d, %4d)", lo, mid, hi);//for test show(destination);//for test return; } merge(source, destination, lo, mid, hi);//merge sorted source to destination } private static void insertionsort(Comparable[] input, int lo, int hi) { for(int i = lo + 1; i <= hi; i++) { for(int j = i; j > lo && less(input[j], input[j-1]); j--) { exch(input, j, j-1); } } StdOut.println("destination:" + input); StdOut.printf("insertionsort(input, %4d, %4d)", lo, hi);//for test show(input);//for test } private static void exch(Comparable[] a, int i, int j) { Comparable t = a[i]; a[i] = a[j]; a[j] = t; } private static void merge(Comparable[] source, Comparable[] destination, int lo, int mid, int hi) { int i = lo; int j = mid + 1; for(int k = lo; k <= hi; k++) { if(i > mid) destination[k] = source[j++]; else if(j > hi) destination[k] = source[i++]; else if(less(source[j], source[i])) destination[k] = source[j++]; else destination[k] = source[i++]; } StdOut.println("source:" + source + " destination:" + destination);//for test StdOut.printf("merge(source, destination, %4d, %4d, %4d)", lo, mid, hi);//for test show(destination);//for test } private static boolean less(Comparable v, Comparable w) { return v.compareTo(w) < 0; } private static void show(Comparable[] a) { //print the array, on a single line. for(int i = 0; i < a.length; i++) { StdOut.print(a[i] + " "); } StdOut.println(); } public static boolean isSorted(Comparable[] a) { for(int i = 1; i < a.length; i++) { if(less(a[i], a[i-1])) return false; } return true; } public static void main(String[] args) { //Read strings from standard input, sort them, and print. String[] input = In.readStrings(); show(input);//for test sort(input); assert isSorted(input); show(input);//for test } }
测试:
M E R G E S O R T E X A M P L E input:[Ljava.lang.String;@1b6d3586 aux:[Ljava.lang.String;@4554617c destination:[Ljava.lang.String;@4554617c insertionsort(input, 0, 7)E E G M O R R S T E X A M P L E destination:[Ljava.lang.String;@4554617c insertionsort(input, 8, 15)E E G M O R R S A E E L M P T X source:[Ljava.lang.String;@4554617c destination:[Ljava.lang.String;@1b6d3586 merge(source, destination, 0, 7, 15)A E E E E G L M M O P R R S T X A E E E E G L M M O P R R S T X
性能比较:
For 20000 random Doubles 1000 trials Merge is 3.6s MergeFasterM is 3.1s MergeUseInsert is 3.2s MergeSkipMerge is 3.5s MergeAvoidCopy is 3.0s MergeX is 2.9s
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决