zoukankan      html  css  js  c++  java
  • 算法(Algorithms)第4版 练习 2.2.9

    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
            
        }
    
    }
    M E R G E S O R T E X A M P L E 
    merge(input,    0,    0,    1)E M R G E S O R T E X A M P L E 
    merge(input,    2,    2,    3)E M G R E S O R T E X A M P L E 
    merge(input,    0,    1,    3)E G M R E S O R T E X A M P L E 
    merge(input,    4,    4,    5)E G M R E S O R T E X A M P L E 
    merge(input,    6,    6,    7)E G M R E S O R T E X A M P L E 
    merge(input,    4,    5,    7)E G M R E O R S T E X A M P L E 
    merge(input,    0,    3,    7)E E G M O R R S T E X A M P L E 
    merge(input,    8,    8,    9)E E G M O R R S E T X A M P L E 
    merge(input,   10,   10,   11)E E G M O R R S E T A X M P L E 
    merge(input,    8,    9,   11)E E G M O R R S A E T X M P L E 
    merge(input,   12,   12,   13)E E G M O R R S A E T X M P L E 
    merge(input,   14,   14,   15)E E G M O R R S A E T X M P E L 
    merge(input,   12,   13,   15)E E G M O R R S A E T X E L M P 
    merge(input,    8,   11,   15)E E G M O R R S A E E L M P T X 
    merge(input,    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 
  • 相关阅读:
    day21
    day19
    【淘淘商城项目】jsonp解决ajax跨域问题
    【淘淘商城项目】商品规格参数的表结构设计
    打印日志的时机
    【javascript】call和apply的区别
    MySQL timestamp自动更新时间
    spring管理属性配置文件properties——PropertiesFactoryBean和PropertyPlaceholderConfigurer的区别
    【Maven】修改nexus默认的工作目录
    URIEncoding和UseBodyEncodingForURI的解释
  • 原文地址:https://www.cnblogs.com/songdechiu/p/6622095.html
Copyright © 2011-2022 走看看