算法(Algorithms)第4版 练习 2.2.26
在sort函数创建aux数组:
package com.qiusongde; import edu.princeton.cs.algs4.In; import edu.princeton.cs.algs4.StdOut; public class MergeNoStaticArray { public static void sort(Comparable[] input) { int N = input.length; Comparable[] aux = new Comparable[N]; sort(input, aux, 0, N-1); } private static void sort(Comparable[] input, Comparable[] aux, int lo, int hi) { if(lo >= hi)//just one entry in array return; int mid = lo + (hi-lo)/2; sort(input, aux, lo, mid); sort(input, aux, mid+1, hi); merge(input, aux, lo, mid, hi); } private static void merge(Comparable[] input, Comparable[] aux, int lo, int mid, int hi) { //copy input[lo,hi] to aux[lo,hi] for(int i = lo; i <= hi; i++) { aux[i] = input[i]; } int i = lo; int j = mid + 1; for(int k = lo; k <= hi; k++) { if(i > mid) input[k] = aux[j++]; else if(j > hi) input[k] = aux[i++]; else if(less(aux[j], aux[i])) input[k] = aux[j++]; else input[k] = aux[i++]; } StdOut.printf("merge(input, %4d, %4d, %4d)", lo, mid, hi); show(input);//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 } }
在merge函数创建aux数组:
package com.qiusongde; import edu.princeton.cs.algs4.In; import edu.princeton.cs.algs4.StdOut; public class MergeArrayCreation { public static void sort(Comparable[] input) { int N = input.length; sort(input, 0, N-1); } private static void sort(Comparable[] input, int lo, int hi) { if(lo >= hi)//just one entry in array return; int mid = lo + (hi-lo)/2; sort(input, lo, mid); sort(input, mid+1, hi); merge(input, lo, mid, hi); } private static void merge(Comparable[] input, int lo, int mid, int hi) { //copy input[lo,hi] to aux[lo,hi] Comparable[] aux = new Comparable[hi-lo+1]; int auxmid = mid - lo; int auxhi = hi - lo; for(int i = lo; i <= hi; i++) { aux[i - lo] = input[i]; } int i = 0;//lo in aux array int j = (auxmid) + 1;//mid + 1 in aux array for(int k = lo; k <= hi; k++) { if(i > auxmid) input[k] = aux[j++]; else if(j > auxhi) input[k] = aux[i++]; else if(less(aux[j], aux[i])) input[k] = aux[j++]; else input[k] = aux[i++]; } StdOut.printf("merge(input, %4d, %4d, %4d)", lo, mid, hi); show(input);//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 } }
性能对比:
package com.qiusongde; import edu.princeton.cs.algs4.StdOut; public class Exercise2226 { public static void main(String[] args) { String alg1 = "MergeNoStaticArray"; String alg2 = "MergeArrayCreation"; int N = 20000; int T = 1000; double t1 = SortCompare.timeRandomInput(alg1, N, T); double t2 = SortCompare.timeRandomInput(alg2, N, T); StdOut.printf("For %d random Doubles %d trials\n", N, T, alg1); StdOut.printf("%s is %.1fs %s is %.1fs", alg1, t1, alg2, t2); } }
For 20000 random Doubles 1000 trials
MergeNoStaticArray is 3.3s MergeArrayCreation is 3.9s