(Solved) : P 758 Implement Experiment Using Techniques Similar Section 41 Test Efficiency N Successiv Q41407887 . . .

P-7.58 Implement an experiment, using techniques similar tothose in section 4.1 to test the efficiency of n successive callsto the add method of an ArrayList, for various n, under each of thefollowing scenarios: a. Each add takes place at index 0. b. Eachadd takes place at index size( ) /2. c. Each add takes place atindex size( ). Analyze your empirical results.

Here are the first two methods, and what the code should looklike.

import java.io.*;
import java.util.*;
public class Main
{
       public static void addFirst(intn)
       {
       ArrayList<Integer> array =new ArrayList<Integer>(n);
       long startingTime =System.currentTimeMillis();
       long currentTime = 0;
       for (int i =1; i<=n; i++)
       {
       array.add(0,i);
       }
       long endTime =System.currentTimeMillis();
       long timeElapsed = (endTime -startingTime);
       System.out.println(“Time whenelements are added to index 0” + endTime);
      
      
       }
       public static void addMiddle(intn)
       {
       ArrayList<Integer> array =new ArrayList<Integer>(n);
       long startingtime =System.currentTimeMillis();
       for (int i = 1; i<= n;i++)
       {
       array.add((array.size()/2),i);
       }
      
       long endTime =System.currentTimeMillis();
       long timeElapsed = (endTime -startingTime);
       System.out.println(“Time whenelements are added in middle” + endTime);
       }
      

Expert Answer


Answer to P-7.58 Implement an experiment, using techniques similar to those in section 4.1 to test the efficiency of n successive …