Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program called InsertionSortBenchmark that automatically generates a tab

ID: 3774727 • Letter: W

Question

Write a program called InsertionSortBenchmark that automatically generates a table of sample run times for the insertion sort algorithm. The program should ask for the smallest and largest value of n and the number of measurements and then execute all sample runs. You should plan to have a method (e.g., "public static int[] generateData(int n)") in your program that generates int arrays of a specific length and populates them with random numbers (use Random's nextInt). This will be needed to run the different tests. To time the running algorithm, you will need to use the StopWatch class (see below). Also below is InsertionSorter.java, which contains an implementation of insertion sort. The algorithm is available as a static method directly from that file.

// StopWatch class

// Insertion Sorter

Explanation / Answer

Hi,

Pleasesee below the InsertionSortBenchmark Class. Please comment/feedback for any queries

Thanks,

Anita

InsertionSortBenchmark.java

import java.util.Random;
import java.util.Scanner;


public class InsertionSortBenchmark {
  
   public static void main(String [] args){
      
       int n=0;
       int intArray[];
           intArray= generateData(n);
   StopWatch stopWatch = new StopWatch();
       InsertionSorter insSort= new InsertionSorter();
      
       stopWatch.reset();
       stopWatch.start();
       insSort.sort(intArray);
       stopWatch.stop();
       System.out.println("Elapsed Time::"+ stopWatch.getElapsedTime());
       long time =stopWatch.getElapsedTime();
  
   }
  
   public static int[] generateData(int n){
      
      
   int intArr[] =new int [n];
   int counter =0;
   int small;
   int large;
   Random rand = new Random();
   Scanner scanner = new Scanner(System.in);
   System.out.print("Enter the smallest value for n : ");
  
   small= new Integer (scanner.nextLine());
System.out.print("Enter the largest value for n : ");
large= new Integer (scanner.nextLine());
  
System.out.print("Enter the no of measurements : ");
n= new Integer (scanner.nextLine());
intArr =new int [n];
   while(counter<n){
  
int num = rand.nextInt(50) +1;
intArr[counter] = num;
counter++;

   }
  
   return intArr;
  
   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote