算法(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

 

转载于:https://www.cnblogs.com/songdechiu/p/6613832.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
个人觉得是我见过的最简单易懂的算法入门书籍。 以前搜刮过几本算法竞赛书,但是难度终归太大【好吧,其实是自己太懒了】。 略翻过教材,大多数水校的教材,大家懂的。好一点的也是那本国内的经典,不是说它写的不好,只是没有这一本好。 本书Java实现,配有大量的图解,没有一句难懂的话,而且全都是模块化实现。 讲的都是实用算法,没有那些高大上听着名字就让人感到很害怕的东西,个人觉得比CLRS实用性要强,更加适合入门的学习。 大一,推荐这本书入门 【有C语言基础即可,自己去搜索下如何用Java写出Hello World就没有问题】 大二,推荐这本书从头到尾好好读一遍,做下上千道的课后习题 【后面的有点小难度,但是难度不大值得一做,听起来很多的样子,用心去做,相信很快就可以做完的】。 大三,推荐这本书,重新温习已知算法,为找工作,考研做准备。 【可以试着自己在纸上全部实现一遍】 大四,依旧推荐这本书,没事重温经典,当手册来查也不错。 Sedgwick 红黑树的发现者,Donald E.Knuth 的得意门生,对各种算法都有比较深入的研究,他的书,我想不会太差。 也许对于数据结构的学习涉及的内容比较少,没有动态规划,图论也只是讲了很基础的东西,字符串中KMP弄的过于复杂(对比于acm)。但是瑕不掩瑜,对于绝大部分内容真的讲的超级清楚,完美的图解,就像单步调试一样,也许是一本不需要智商就能看懂的算法书(习题应该略有难度,还没有做,打算上Princeton的公开课时同步跟进)。至少这是一本让我这个算法渣渣看了爱不释手,怦然心动的书。 完美学习资源: 官方主页:http://algs4.cs.princeton.edu/home/ Coursera公开课:https://www.coursera.org/course/algs4partI (听说已经开课两期了,最近即将开课的时间是2014/09/05号那期,希望有兴趣的同学一起来学习)。 MOOC平台(笔记、讨论等): http://mooc.guokr.com/course/404/Algorithms--Part-I/ http://mooc.guokr.com/course/403/Algorithms--Part-II/ 不得不吐槽,他的lecture比他的书好,他本人讲的课更是一绝。 互补课程: 斯福坦的Algorithms: Design and Analysis, http://mooc.guokr.com/course/157/Algorithms--Design-and-Analysis--Part-1/ 快毕业了才接触到豆瓣和MOOC,看到很多经典的书籍都是推荐大学一二年级的学生看,每每想到自己却连书皮都没有摸过,就深感惭愧。 我们都老的太快,却聪明得太迟。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值