• Algs4-2.1.38不同类型的元素


    2.1.38不同类型的元素。编写一个测试用例,生成由多种数据类型元素组成的数组,元素的主键值随机,包括:
    1)每个元素的主键均为String类型(至少长10个字符),并含一个double值;
    2)每个元素的主键均为double类型,并含有10个String值(每个都至少长10个字符);
    3)每个元素的主键均为int类型,并含有一个int[20]值
    评估并验证这些输入数据对本节讨论的算法的性能影响。
    答:希尔排序性能表现仍就很好。选择和插入对String型主键排序相对Double与Integer型主键花费时间约2至3倍。
    1)String主键
    图片
    2)Double主键
    图片
    3)Integer主键
    图片

    import java.util.Arrays;
    public class E2d1d38
    {
        /*
        private static class Node implements Comparable<Node>
        {
            String key;
            Double value;
            public int compareTo(Node other)
            {
              return key.compareTo(other.key);
            }
        }*/
        /*
         private static class Node implements Comparable<Node>
        {
           Double key;
           String value;
            public int compareTo(Node other)
            {
              return key.compareTo(other.key);
            }
        }
         */
         private static class Node implements Comparable<Node>
        {
           Integer key;
           int[] value=new int[10];
            public int compareTo(Node other)
            {
              return key.compareTo(other.key);
            }
        }
        public static double time (String alg,Node[] a)
        {
            Stopwatch timer =new Stopwatch();
            if(alg.equals("Insertion")) Insertion.sort(a);
            if(alg.equals("Selection")) Selection.sort(a);
            if(alg.equals("Shell")) Shell.sort(a);
          // if(alg.equals("Merge")) Merge.sort(a);
          //  if(alg.equals("Quick")) Quick.sort(a);
          //  if(alg.equals("Heap")) Heap.sort(a);
            return timer.elapsedTime();
        }
       
        public static double timeRandomInput(String alg,int N,int T)
        {
            double total =0.0;
            Node[] a=new Node[N];
              for (int t=0;t<T;t++)
                {
                    for(int i=0;i<N;i++)
                    {
                        Node item= new Node();
                        item.key=StdRandom.uniform(-N,N);
                        for(int j=0;j<item.value.length;j++)
                           item.value[j]=StdRandom.uniform(-N,N);
                       
                        a[i]=item;
                        /*StdOut.println("---"+a[i].key);
                        for(int j=0;j<item.value.length;j++)
                            StdOut.println(a[i].value[j]);
                            */
                    }
                    total+=time(alg,a);
                }
           return total;
        }
      
           
        public static void main(String[] args)
        {
         
            Integer N=Integer.parseInt(args[0]);
            Integer T=Integer.parseInt(args[1]);
       
     
             Double time;
             time=timeRandomInput("Insertion",N,T);
             StdOut.printf("Insertion For %d  items spend time=%.2f ",N,time);

             time =timeRandomInput("Selection",N,T);
             StdOut.printf("Selection For %d  items spend time=%.2f ",N,time);
            
             time =timeRandomInput("Shell",N,T);
             StdOut.printf("Shell For %d  items spend time=%.2f ",N,time);
           

        }
    }
  • 相关阅读:
    程序打包
    MFC AfxMessageBox默认标题修改
    Json
    agsXMPP
    xmpp
    afxcomctl32.h与afxcomctl32.inl报错
    jQuery使用
    EChart使用
    C++ tinyXML使用
    electron之Windows下使用 html js css 开发桌面应用程序
  • 原文地址:https://www.cnblogs.com/longjin2018/p/9860071.html
Copyright © 2020-2023  润新知