用另外2个数组填充2d数组

时间:2012-11-02 03:45:20

标签: java for-loop complexity-theory multidimensional-array

    void some_function() {
     int d[] = new int[10];
     int e[] = new int[15]; 
     int f[] = new int[20];
     int g[] = new int[25]; 
     int h[] = new int[30];
     int i[] = new int[35];
     int j[] = new int[40];
     int k[] = new int[45];
     int l[] = new int[50];
     int m[] = new int[55];
     int n[] = new int[60];
     int o[] = new int[65];
     int p[] = new int[70];
     int q[] = new int[75];
     int r[] = new int[80];
     int s[] = new int[85];
     int t[] = new int[90];
     int u[] = new int[95];
     int v[] = new int[100];
    //Randomly generate int elements of the array
     int master_list[][] = {d, e, f, g, h, i, j, k, l, m, n, 
        o, p, q, r, s, t, u, v};
     Random gen = new Random();
     int i_array[];
     double run_times[][] = new double[19][6];
     double start, end, total_time_1, total_time_2, total_time_3;
     double comp_1, comp_2, comp_3;
     for(int idex = 0; idex < master_list.length; idex++) {
        i_array = master_list[idex];
        //Randomly generate integers to put in each array        
        for(int jdex = 0; jdex < master_list[idex].length; jdex++) {
           i_array[jdex] = gen.nextInt(10);
        }

        comp_1 = ((14*(Math.pow(i_array.length, 2))) 
           + (i_array.length/2) + 4);
        comp_2 = ((14*i_array.length) + 35);
        comp_3 = ((19*i_array.length) + 4);
        /* This is the part where we begin to print and calculate
           data for our report. It begins with caluculating the
            run time of each algorithm by calling the system clock
            method of System.nanoTime() */
        System.out.println("Input size: " + i_array.length);
        System.out.println("Run time of Algorithm 1:");        
        start = System.nanoTime();
        driver.algorithm_1(i_array); 
        end = System.nanoTime();
        total_time_1 = end - start;
        System.out.println("Total time elapsed: " + total_time_1
           + " milliseconds.");
        //We then add this time into the [19][6] matrix.
        run_times[idex][0] = total_time_1;
        run_times[idex][3] = comp_1;

        //The process then repeats for algorithm 2
        System.out.println("Run time of Algorithm 2:"); 
        start = System.nanoTime();
        driver.algorithm_2(i_array); 
        end = System.nanoTime();
        total_time_2 = end - start;
        System.out.println("Total time elapsed: " + total_time_2
           + " milliseconds.");
        run_times[idex][1] = total_time_2; 
        run_times[idex][4] = comp_2; 

        //Yet again another repitition for algorithm 3
        System.out.println("Run time of Algorithm 3:");
        start = System.nanoTime();
        driver.algorithm_3(i_array); 
        end = System.nanoTime();
        total_time_3 = end - start;
        System.out.println("Total time elapsed: " + total_time_3
           + " milliseconds.\n");
        run_times[idex][2] = total_time_3;
        run_times[idex][5] = comp_3;
     }
  }

所以整个事情的问题是我正在尝试创建一个大小为run_times的数组[19] [6],但是,我只得到一个大小列表[10] [5]。

因此,算法采用int数组d-v并为它们生成随机元素。然后我计算每个算法的运行时间和复杂性(不包括在内)。

运行时数组是这样的:      [0] [0] = algorithm_1时间      [0] [1] = algorithm_2时间      [0] [2] = algorithm_3时间      [0] [3] = algorithm_1复杂性      [0] [4] = algorithm_2复杂度      [0] [5] = algorithm_3复杂度

复杂性加倍是将数组大小插入T(n)递归关系中的n。

知道什么是错的吗?它编译并且没有运行时错误。

0 个答案:

没有答案