Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please replace the Bubble sort with merge sort in the Code. It is suppose to time the amount of time it takes to sort an
Please replace the Bubble sort with merge sort in the Code. It is suppose to time the amount of time it takes to sort an array of 10,000 random objects to sort. Then repeat if for a total of 5 run times. Please put the correct code back in working in Java thank you.
import java.io.*; public class BubbleSort { static File diskFile; static PrintWriter pw; //method that will sort the array public static void bubbleSort(int[] arr) { // sort the array using bubble sort int n = arr.length; // length of array in n variable for (int i = 0; i < n - 1; i++) // loop variable array -1 size for (int j = 0; j < n - i - 1; j++) // loop array variable -i-1 if (arr[j] > arr[j + 1]) // check if array of j index value is greater than array of j+1 { // swap temp and arr[i] int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } public static void main(String[] args) { System.out.println("Start bubble sort "); startBubbleSort(); System.out.println("End Bubble sort "); } public static void startBubbleSort() { int number_of_tests; number_of_tests = 5; int size = 10000; int[] array = new int[size]; long startTime = 0; long endTime = 0; long duration = 0; try { diskFile = new File("bubble data"); try { pw = new PrintWriter(diskFile); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } pw.println("Test Number\tSmaple Size\tTime"); for (int currentTest = 1; currentTest <= number_of_tests; currentTest = currentTest + 1) { System.out.println("Starting Time: " + currentTest); for (int i = 0; i < array.length; i++) { array[i] = (int) (Math.random() * size + 1); } startTime = System.nanoTime(); System.out.println("Sorting Array for Test : " + currentTest + " "); bubbleSort(array); endTime = System.nanoTime(); duration = endTime - startTime; System.out.println("Current Test: " + currentTest + ","); System.out.print("Sample Size:" + size + ","); System.out.print("Time to complete Bubble Sort: "); System.out.printf("%12.5f %n", (double) duration / 1000000000); printReportItem(duration, size, currentTest, pw); } } finally { pw.close(); } } public static void printReportItem(long duration, int size, int testNumber, PrintWriter pw) { double time = duration / 1000000000.00; pw.print(testNumber + "\t\t\t"); pw.print(size + "\t\t"); pw.println(time); // System.out.println("Current Test: " + currentTest + ","); System.out.print("Sample Size:" + size + ","); System.out.print("Time to complete Bubble Sort: "); System.out.printf("%12.5f %n", (double) duration / 1000000000); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started