Menu

4 10 Pts Merge Sort Vs Insertion Sort Running Time Analysis Modify Code Verified Code Runs Q43839659

4) (10 pts) Merge Sort vs Insertion Sort Running time analysis a) Modify code-Now that you have verified that your code runs

10:17 / InsertionSort.java: Java program to sort integers using Insertion sort import java.io.File; import java.io.FileWriter1 ledu SIZE L UCI U LICCIS ILLICC for(int i=0;i<size;i++) data[i] = fileScan.nextInt(): //apply insertion sort to the array i2. 1 mid= (low+high) 2; // get the index of mid-alue o rray merge Sort(arr,low,mid); // apply merge sort in left half of arraW save the sorted array back to original array for(i low;i< high;i++) arr[i] = b[i]; public static void main(String[] args)POException Scanner fileScan = new Scanner(new File(data.txt) open the input file, provide full path to file File Writer me4) (10 pts) Merge Sort vs Insertion Sort Running time analysis a) Modify code-Now that you have verified that your code runs correctly using the data.txt input file, you can modify the code to collect running time data. Instead of reading arrays from the file data.txt and sorting, you will now generate arrays of size n containing random integer values from 0 to 10,000 to sort. Use the system clock to record the running times of each algorithm for ten different values of n for example: n = 5000, 10000, 15000, 20,000, …, 50,000. You may need to modify the values of n if an algorithm runs too fast or too slow to collect the running time data (do not collect times over a minute). Output the array size n and time to the terminal. Name these new programs insertTime and mergeTime. Submit a copy of the timing programs to TEACH in the Zip file from problem 3. b) Collect running times – Collect your timing data on the engineering server. You will need at least eight values of t (time) greater than 0. If there is variability in the times between runs of the same algorithm you may want to take the average time of several runs for each value of n. Create a table of running times for each algorithm. c) Plot data and fit a curve – For each algorithm plot the running time data you collected on an individual graph with n on the X-axis and time on the y-axis. You may use Excel, Matlab, R or any other software. What type of curve best fits each data set? Give the equation of the curves that best “fits” the data and draw that curves on the graphs. d) Combine – Plot the data from both algorithms together on a combined graph. If the scales are different you may want to use a log-log plot. e) Comparison – How does your experimental running times compare to the theoretical running times of the algorithms? 10:17 / InsertionSort.java: Java program to sort integers using Insertion sort import java.io.File; import java.io.FileWriter: import java.io.IOException; import java.util.Scanner, public class Insertion Sort Il method to sort the array of integers in ascending order using insertion sort public static void insertion Sortint numArray[ intij.key: // loop from 1 to end of array for i=1;i<numArray.length;++i) key = numArray[i]; // get the ith integer in key //loop continues tillj!= -1 or we get th value in array <key while(>-0 && numArray[] >key) numArray[+1) = numArray[i]; numArray[i+1] =key; public static void main(String[] args) throws IOException Scanner fileScan = new Scanner(new File(“data.txt”)) open the input file, provide full path to file FileWriter in Writer = new FileWriter(“insert.txt”); // create and open the output file for insertion sort int size: int data); W loop till the end of input file while(fileScan.hasNext) size = (int) (fileScan.nextDouble(); // read the number of integers in the line (may be double, so convertit into int after reading it as double) // create the array of size data-new int[size] 1/ read size number of integers in the line for(int i=0;i<size;i++) data[i] = fileScan.nextInt(): 1 ledu SIZE L UCI U LICCIS ILLICC for(int i=0;i<size;i++) data[i] = fileScan.nextInt(): //apply insertion sort to the array insertion Sort(data): // output the data back to output file for(int i=0;i<size-1;i++) in Writer.write(data[i]+””); in Writer.write(data[size-1]+”n”); // close the files ) fileScan.close(); end of Insertion Sort.java in Writer.close(); } Output: Input file: data.txt : Data in a line are separated by a single space 10 10 9 8 7 6 5 4 3 2 1 12 10 10 10 10 10 10 10 10 10 10 10 10 11 19 22 3 8 19 8 22 19 8 11 9 1. 100 2. 56 Output file: insert.txt 1 2 3 4 5 6 7 8 9 10 10 10 10 10 10 10 10 10 10 10 10 10 3 888 9 11 19 19 19 22 22 100 56 MergeSort.java: Java program to sort integers using Merge sort import java.io.File: import java.io.File Writer, import java.io.IOException; import java.util.Scanner, public class Merge Sort // method to sort the array of integers using merge sort recursively public static void mergeSortint arr[)int low.int high) int mid: // array contains atleast one element if(low<high) mid – (low high) 2: //get the index of mid value of ray merge Sort(arr,low,mid); // apply merge sort in left half of array 2. 1 mid= (low+high) 2; // get the index of mid-alue o rray merge Sort(arr,low,mid); // apply merge sort in left half of array merge Sort(arr.mid+1, high); //apply merge sort in right half of array mergeArr(arr,low.high,mid); // merge the left and right half // method to merge the left and right half of the array, so as to get the sorted array public static void mergeArr(int an.int low int high int mid) int i = low; int h=mid+1; int i-low; int brr[] new int[arr.length); W loop to merge the sub arrays while(l<-mid &&h=high) if(an[1] <arr[h]) bry[i] = arr[U else ber[i]=anch): h+ : while(l mid) brrſi] = arr[U]; while(h high) bri++] = arr(h++]; 1/ save the sorted array back to original array for(i=low;i<=high;i++) arr[i] -bri): W save the sorted array back to original array for(i “low;i< high;i++) arr[i] = b[i]; public static void main(String[] args) throws IOException Scanner fileScan = new Scanner(new File(“data.txt”)) open the input file, provide full path to file File Writer merge Writer = new FileWriter(“merge.txt”); // create and open the output file for merge sort int size: int datal: // loop till the end of input file while(fileScan.hasNext() size = (int) (fileScan.nextDoublc(); // read the number of integers in the line (may be double, so convertit into int after reading it as double) // create the array of size data-new int[size]; // read size number of integers in the line for(int i=0;i<size;i++) data[i] = fileScan.nextInt(); // apply merge sort to the array merge Sort(data,0,size-1); Il output the data back to output file for(int i=0;i<size-1;i++) merge Writer.write(data[i]+””); merge Writer.write(data[size-1]+”n”); 1/ close the files fileScan.close(); merge Writer.close(); end of MergeSort.java Output: Input file: data.txt : Data in a line are separated by a single space 10 10 9 8 7 6 5 4 3 2 1 12 10 10 10 10 10 10 10 10 10 10 10 10 11 19 22 3 8 19 8 22 19 8 11 9 1. 100 2. 56 POException Scanner fileScan = new Scanner(new File(“data.txt”) open the input file, provide full path to file File Writer merge Writer = new File Writer(“merge.txt”); // create and open the output file for merge sort int size: int datal: // loop till the end of input file while(fileScan.hasNext) size = (int)(fileScan.nextDouble(); // read the number of integers in the line (may be double, so convertit into int after reading it as double) Il create the array of size data-new int[size]: // read size number of integers in the line for(int i=0;i<size;i++) data[i] = fileScan.nextInt(): // apply merge sort to the array merge Sort(data 0.size-1); // output the data back to output file for(int i 0;i<size-1.i++) merge Writer.write(data[i]+””); merge Writer.write(data[size-1]+”n”); // close the files fileScan.close(): merge Writer.close(); end of MergeSort.java Output: Input file: data.txt : Data in a line are separated by a single Красе 10 10 9 8 7 6 5 4 3 2 1 12 10 10 10 10 10 10 10 10 10 10 10 10 11 19 22 3 8 19 8 22 19 8 119 1. 100 2. 56 Output file : merge.txt 1 2 3 4 5 6 7 8 9 10 10 10 10 10 10 10 10 10 10 10 10 10 3 888 9 11 19 19 19 22 22 5 6 Show transcribed image text 4) (10 pts) Merge Sort vs Insertion Sort Running time analysis a) Modify code-Now that you have verified that your code runs correctly using the data.txt input file, you can modify the code to collect running time data. Instead of reading arrays from the file data.txt and sorting, you will now generate arrays of size n containing random integer values from 0 to 10,000 to sort. Use the system clock to record the running times of each algorithm for ten different values of n for example: n = 5000, 10000, 15000, 20,000, …, 50,000. You may need to modify the values of n if an algorithm runs too fast or too slow to collect the running time data (do not collect times over a minute). Output the array size n and time to the terminal. Name these new programs insertTime and mergeTime. Submit a copy of the timing programs to TEACH in the Zip file from problem 3. b) Collect running times – Collect your timing data on the engineering server. You will need at least eight values of t (time) greater than 0. If there is variability in the times between runs of the same algorithm you may want to take the average time of several runs for each value of n. Create a table of running times for each algorithm. c) Plot data and fit a curve – For each algorithm plot the running time data you collected on an individual graph with n on the X-axis and time on the y-axis. You may use Excel, Matlab, R or any other software. What type of curve best fits each data set? Give the equation of the curves that best “fits” the data and draw that curves on the graphs. d) Combine – Plot the data from both algorithms together on a combined graph. If the scales are different you may want to use a log-log plot. e) Comparison – How does your experimental running times compare to the theoretical running times of the algorithms?
10:17 / InsertionSort.java: Java program to sort integers using Insertion sort import java.io.File; import java.io.FileWriter: import java.io.IOException; import java.util.Scanner, public class Insertion Sort Il method to sort the array of integers in ascending order using insertion sort public static void insertion Sortint numArray[ intij.key: // loop from 1 to end of array for i=1;ikey) numArray[+1) = numArray[i]; numArray[i+1] =key; public static void main(String[] args) throws IOException Scanner fileScan = new Scanner(new File(“data.txt”)) open the input file, provide full path to file FileWriter in Writer = new FileWriter(“insert.txt”); // create and open the output file for insertion sort int size: int data); W loop till the end of input file while(fileScan.hasNext) size = (int) (fileScan.nextDouble(); // read the number of integers in the line (may be double, so convertit into int after reading it as double) // create the array of size data-new int[size] 1/ read size number of integers in the line for(int i=0;i

Expert Answer


Answer to 4) (10 pts) Merge Sort vs Insertion Sort Running time analysis a) Modify code-Now that you have verified that your code …

OR