Question: Using the Sorting.java file, modify the program to investigate the behavior ( best , worst, average ) of insertion sort and three diminishing increments for

"Using the Sorting.java file, modify the program to investigate the behavior (best, worst, average) of insertion sort and three diminishing increments for Shell sort. Run your program for a range of array sizes. Record the data that you obtain in a table in a Word document. Write a narrative describing your findings. Turn in the Word doc." make an insertion sort graph and a shell sort graph, specifically for the average case. graph swaps over array size. for the best case, no graph is needed, but a write couple of sentences explaining why there are no swaps. for the worst case, create an analysis and a graph. import java.util.Random; import java.util.Scanner; public class Sorting { public static void main(String[] args){ int increments[]= null; Scanner scr = new Scanner(System.in); long swaps; long beginTime, endTime; while(true){ int n = scr.nextInt(); if(n<0) break; int[] A = new int[n]; long seed = scr.nextLong(); //Insertion sort increments = new int[1]; increments[0]=1; initArray(A,seed); beginTime = System.nanoTime(); swaps = shellSort(A,increments); endTime = System.nanoTime(); System.out.println("Insertion sort swaps="+ swaps +" time="+(endTime-beginTime)/1000000+"ms"); //Shell sort with powers of 2 increments int numIncr =(int)(Math.log(n)/Math.log(2)); increments = new int[numIncr]; increments[numIncr-1]=1; for(int i=numIncr-2; i>=0; i--){ increments[i]=2* increments[i+1]; } initArray(A,seed); beginTime = System.nanoTime(); swaps = shellSort(A,increments); endTime = System.nanoTime(); System.out.println("Shell sort powers of 2 swaps="+ swaps +" time="+(endTime-beginTime)/1000000+"ms"); //Shell sort with 2^k-1 increments increments = new int[numIncr-1]; increments[numIncr-2]=1; int power =2; for(int i=numIncr-3; i>=0; i--){ power *=2; increments[i]= power -1; } initArray(A,seed); beginTime = System.nanoTime(); swaps = shellSort(A,increments); endTime = System.nanoTime(); System.out.println("Shell sort with 2^k-1 increments swaps="+ swaps +" time="+(endTime-beginTime)/1000000+"ms"); //Shell sort with (3^k-1)/2 increments numIncr =(int)(Math.log(n)/Math.log(3)); increments = new int[numIncr]; increments[numIncr-1]=1; power =3; for(int i=numIncr-2; i>=0; i--){ power *=3; increments[i]=(power-1)/2; } initArray(A,seed); beginTime = System.nanoTime(); swaps = shellSort(A,increments); endTime = System.nanoTime(); System.out.println("Shell sort with (3^k-1)/2 increments swaps="+ swaps +" time="+(endTime-beginTime)/1000000+"ms"); } scr.close(); } static void initArray(int[] A, long seed){ Random rnd = new Random(seed); for (int i=0; i= incr && (A[j]

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!