zoukankan      html  css  js  c++  java
  • Algs4-2.2.21一式三份

    2.2.21一式三份。给定三个列表,每个列表中包含N个名字,编写一个线性对数级别的算法来判定三份列表中是否含有公共的名字,如果有,返回第一个被找到了这种名字。
    算法:
    1)先将三个列表使用归并排序方法排序,形成小元素在前,大元素在后的排列。排序需要O(NlgN)时间复杂度。
    2)当三个列表的最顶元素相同时说明已经找到重复项。
    3)当三个列表的最顶元素不相同时,那么最早出现的相同元素只有可能在这三个元素中最大元素的位置或更靠后的位置,由于这个原因所以定位到三个列表中不小于这个最大元素的位置,此时如果三个列表的顶元素相同那么说明找到重复项,如果不同还需要找出这三个顶元素的最大者,然后继续向列表靠后的位置查找,直到找到相同元素或是某一个列表的最后位置。
    public class E2d2d21
    {
        private static int subArrayLenTrunONInsertionSort=15;
        public static void sort(Comparable[] a)
        {
            int len=a.length;
            Comparable[] aux=new Comparable[len];
            for(int i=0;i<len;i++)
                aux[i]=a[i];
            sort(a,aux,0,a.length-1);
       }
       
        private static void sort(Comparable[] a,Comparable[] aux,int lo,int hi)
        {
            if ((hi-lo+1)<=subArrayLenTrunONInsertionSort)
            {
              insertionSort(a,lo,hi);
            }
            else
            {
              int mid=lo+(hi-lo)/2;
              sort(aux,a,lo,mid);
              sort(aux,a,mid+1,hi);
           
              if(!less(aux[mid+1],aux[mid]))
              {
                  for(int i=lo;i<=hi;i++)
                      a[i]=aux[i];
                  return;
              }
              merge(aux,a,lo,mid,hi);
            }
        }

       
        private static void merge(Comparable[] a,Comparable[] aux,int lo,int mid,int hi)
        {
          int i=lo;
          int j=mid+1;
          for(int k=lo;k<=hi;k++)
          {
               if (i>mid) aux[k]=a[j++];
               else if (j>hi) aux[k]=a[i++];
               else if (less(a[j],a[i])) aux[k]=a[j++];
               else                           aux[k]=a[i++];
          }
        }
        private static boolean less(Comparable v,Comparable w)
        { return v.compareTo(w)<0;}
       
        private static void exch(Comparable[] a,int i,int j)
        {
          Comparable t=a[i];
          a[i]=a[j];
          a[j]=t;
        }
       
        private static void insertionSort(Comparable[] a,int lo,int hi)
        {
          for (int i=lo+1;i<=hi;i++)
            for (int j=i;j>lo && less(a[j],a[j-1]);j--)
                exch(a,j,j-1);
        }
       
        private static Comparable maxThree(Comparable a,Comparable b,Comparable c)
        {
            Comparable max=a;
            if(max.compareTo(b)<0) max=b;
            if(max.compareTo(c)<0) max=c;
            return max;
        }
       
        public static Comparable repeartFirst(Comparable[] a,Comparable[] b,Comparable[] c)
        {
            sort(a);
            sort(b);
            sort(c);
           //
            int aLen=a.length;
            int bLen=b.length;
            int cLen=c.length;
            //
            int aIndex=0;
            int bIndex=0;
            int cIndex=0;
            //
            Comparable max;
            while(aIndex<aLen && bIndex<bLen && cIndex<cLen)
            {
                if (a[aIndex]==b[bIndex] && a[aIndex]==c[cIndex])  return a[aIndex];
                max=maxThree(a[aIndex],b[bIndex],c[cIndex]);
                while(aIndex<aLen && less(a[aIndex],max)) aIndex++;
                while(bIndex<bLen && less(b[bIndex],max)) bIndex++;
                while(cIndex<cLen && less(c[cIndex],max)) cIndex++;
            }
            return "none.";
        }
       
     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)
     {
         Comparable[] a={"kz","ae","aa","ad","ac","al"};
         Comparable[] b={"bz","bb","aa","bd","bc","kz"};
         Comparable[] c={"cz","aa","kz","cd","cc","ab"};
        
         StdOut.printf("%s",repeartFirst(a,b,c));
     }
    }

  • 相关阅读:
    The type android.support.v4.app.TaskStackBuilder$SupportParentable cannot be resolved.
    Errors running builder 'Android Pre Compiler' on project
    Android SDK Version 对应的 rom 版本
    随手记Note—团队总结汇报
    第四次团队作业
    随手记note(第三次团队作业)
    随手记note(第二次团队作业)
    随手记note(记事簿)
    小学生四则运算生成器
    软件工程结对编程作业
  • 原文地址:https://www.cnblogs.com/longjin2018/p/9860114.html
Copyright © 2011-2022 走看看