zoukankan      html  css  js  c++  java
  • Algs4-2.2.25-2多向归并排序-第二版代码-微小改进

    2.2.25多向归并排序。实现一个k向(相对双向而言)归并排序程序。分析你的算法,估计最佳的k值并通过实验验证猜想。
    在第一版的基础上为减少检查子数组是否已结束时的对比,做的微小改进。
    public class E2d2d25d2
    {
        private static Comparable[] aux;
        public static void sort(Comparable[] a,int k)
        {
            aux=new Comparable[a.length];
            sort(a,0,a.length-1,k);
        }
       
        public static void sort(Comparable[] a,int lo,int hi,int k)
        {
            if (hi<=lo) return;
            int loNew,hiNew;
            loNew=lo;
            for (int i=0;i<k;i++)
            {
                hiNew=loNew+(hi-loNew)/(k-i);
                sort(a,loNew,hiNew,k);
                loNew=hiNew+1;
            }
            merge(a,lo,hi,k);
        }
       
      
        public static void merge(Comparable[] a,int lo,int hi,int k)
        {
            //
            for (int i=lo;i<=hi;i++)
                 aux[i]=a[i];
            //
            Integer[][] b=new Integer[k][2];
            int loNew,hiNew;
            loNew=lo;
            for (int i=0;i<k;i++)
            {
                hiNew=loNew+(hi-loNew)/(k-i);
                b[i][0]=loNew;
                b[i][1]=hiNew;
                loNew=hiNew+1;
            }
            //index=-1 sub array is end
            for(int j=0;j<k;j++)
               if(b[j][0]>b[j][1] || b[j][0]>hi) b[j][0]=-1;
           
            int minIndex=-1;   
            for(int i=lo;i<=hi;i++)
            {
                //find the 1st sub array's top index from k sub array to the minIndex
                for(int j=0;j<k;j++)
                    if(b[j][0]>-1) {minIndex=j;break;}
                //find the minValue's index from  elements of k sub array's top elements.
                for(int j=0;j<k;j++)
                {
                    if (b[j][0]>-1)
                       if(less(aux[b[j][0]],aux[b[minIndex][0]])) minIndex=j;
                }
                //aux merge to a
                a[i]=aux[b[minIndex][0]];
                //minvalue sub array top--
                b[minIndex][0]++;
                //if sub array is end then set -1
                if(b[minIndex][0]>b[minIndex][1] || b[minIndex][0]>hi) b[minIndex][0]=-1;
            }
          }
      
        private static boolean less(Comparable v,Comparable w)
        { return v.compareTo(w)<0;}

         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)
         {
             int K=Integer.parseInt(args[0]);
             int N=Integer.parseInt(args[1]);
             Comparable[] a=new Comparable[N];
            
             for(int i=0;i<N;i++)
                 a[i]=StdRandom.uniform();
             //
            sort(a,K);
            StdOut.printf("isSorted=%s",isSorted(a));
         }
    }
  • 相关阅读:
    模板实参演绎
    模板实例化
    模板中的名称
    友元函数在类中的声明在外围是不可见的
    C++ 宽字符(wchar_t)与窄字符(char)的转换
    ImageButton如何让图片按比例缩放不被拉伸
    C++模板实例化(1)
    android开发之GenyMotion与intelliJ的配置
    jacoco报告表头注释
    Spring源码工具类之StringUtils
  • 原文地址:https://www.cnblogs.com/longjin2018/p/9860136.html
Copyright © 2011-2022 走看看