Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Helpppp I created algorithm for mergeSort and algorithm for insertionSort for 7 different txt file . I need to get recorded time for merge

Java Helpppp

I created algorithm for mergeSort and algorithm for insertionSort for 7 different txt file . I need to get recorded time for merge sort and insertion sort for the 7 txt files. Modify the merge sort algorithm such that when array size gets small enough, you would invoke insertion sort instead of merge sort (you have to change the base condition in recursion) (and give recorded time for insertionSort and mergeSort so I can do comparison). Instead of hardcoding the array size make it a variable that you can pass as an argument to the merge sort method and test this cutoff value with atleast 4 different values for each of the 7 txt files. Please format the code properly. Thanks

This is my professor's response to help:

Basically, for this modified merge sort function, you need to add a new parameter x to merge sort such that, if the size of the unsorted part of the array is x or less (i.e. if r - p <= x), the merge sort method runs insertion sort instead of the normal merge sort algorithm. So your 4 values of x that you test can be any numbers that you want (like 75 or 250 or 3141), not just the numbers of the input text files . To figure out a good small enough size for x, you should compare the runtimes (form Problem 3) of both insertion sort and merge sort on all of the input text files (100 - 500,000) and take note of how efficient insertion sort is compared to merge sort. Then, you can choose 4 different values of x that you think would make the modified merge sort method run more efficiently than the original merge sort method, and then run the modified merge sort method with each of the four xs (cutoffs) that you chose for each of the seven input array sizes. Let me know if that process makes sense.

Here's the code:

import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class mergeSort { public static void main(String args[]) throws FileNotFoundException { int arr[], temp[]; String Filename[] = new String[] {"input_100.txt", "input_1000.txt", "input_5000.txt", "input_10000.txt", "input_50000.txt", "input_100000.txt", "input_500000.txt"}; // Save filenames in a variable double times[] = new double[Filename.length]; int file1 = 0, file2 = 0, file3 = 0, file4 = 0, file5 = 0, file6 = 0, file7 = 0; Scanner number; for (int n = 0; n < Filename.length; n++) { int mySum = 0; number = new Scanner(new File(Filename[n])); while (number.hasNextInt()) { int x = number.nextInt(); // save values from file mySum++; // increments the value by 1 for each new value } // close the scanner if (n == 0) { file1 = mySum; } else if (n == 1) { file2 = mySum; } else if (n == 2) { file3 = mySum; } else if (n == 3) { file4 = mySum; } else if (n == 4) { file5 = mySum; } else if (n == 5) { file6 = mySum; } else if (n == 6) { file7 = mySum; } number.close(); } int index[] = {file1, file2, file3, file4, file5, file6, file7}; for (int n = 0; n < Filename.length; n++) { arr = new int[index[n]]; temp = new int[index[n]]; long beginTime, finishTime, totalTime; // declare variables to save time System.out.println("File"); number = new Scanner(new File(Filename[n])); if (n == 0) { for (int i1 = 0; i1 < file1; i1++) arr[i1] = Integer.parseInt(number.next()); } else if (n == 1) { for (int i2 = 0; i2 < file2; i2++) arr[i2] = Integer.parseInt(number.next()); } else if (n == 3) { for (int i3 = 0; i3 < file3; i3++) arr[i3] = Integer.parseInt(number.next()); } else if (n == 4) { for (int i4 = 0; i4 < file4; i4++) arr[i4] = Integer.parseInt(number.next()); } else if (n == 4) { for (int i5 = 0; i5 < file5; i5++) arr[i5] = Integer.parseInt(number.next()); } else if (n == 5) { for (int i6 = 0; i6 < file6; i6++) arr[i6] = Integer.parseInt(number.next()); } else if (n == 6) { for (int i7 = 0; i7 < file7; i7++) arr[i7] = Integer.parseInt(number.next()); } beginTime = System.nanoTime(); finishTime = System.nanoTime(); totalTime = finishTime - beginTime; times[n] = totalTime; System.out.println("Recorded time : " + times[n] + " nanosecond to sort file " + Filename[n]); number.close(); merge_sorting(arr, temp, 0, index[n] - 1); // runs insertion sort algorithm for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println(" "); } System.out.println(" Sorting complete"); } // function to sort array using merge sort public static void merge_sorting( int arr[], int temp[], int low, int high) { // lower index and higher index as argument to the function if (low < high) { // calculating if low>high then the array will be sorted // else sort the array int mid = low + (high - low) / 2; // calculate mid index to break the array into two halves merge_sorting(arr, temp, low, mid); // calling sorting function to sort the left subarray merge_sorting(arr, temp, mid + 1,high); // calling sorting function to sort the right subarray merge(arr, temp, low, mid,high); // calling function to merge two subarrays } } public static void merge(int arr[], int temp[], int low, int m, int high) { // merging function for (int i = 0; i <= high; i++) { // copying the value of the array in a temporary array ar1 temp[i] = arr[i]; } int i = low, j = m + 1, k = low; // initializing variable while (i <= m && j <= high) { // checking if the value of i is less than mid index and value of j is less than higher index if (temp[i] <= temp[j]) { // swap the values arr[k] = temp[i]; // put the sorted value in the original array i++; // increment loop variable } else { arr[k] = temp[j]; // copy content of temporary variable to original // array j++; // increment loop variable } k++; } while (i <= m) { // if value of i is less than the mid index then copy value of temporary array to the original array arr[k] = temp[i]; k++; // increment loop variable i++; } } public static void insertionSort(int array[]) { int n = array.length; // declare array length for (int j = 1; j < n; j++) { // intialize the loop to sort int key = array[j]; int i = j - 1; while ((i > -1) && (array[i] > key)) { array[i + 1] = array[i]; i--; } array[i + 1] = key; // the first element is given the smallest value } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Object Oriented Databases Prentice Hall International Series In Computer Science

Authors: John G. Hughes

1st Edition

0136298745, 978-0136298748

Students also viewed these Databases questions