Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Download the Java code for Bubble and Merge Sorts attached. Modify the code by adding about 3-4 more numbers to each sort. Add in imports

Download the Java code for Bubble and Merge Sorts attached. Modify the code by adding about 3-4 more numbers to each sort. Add in imports of the Random and Date (or DateTime) libraries. Modify the code so it can sort an array of random numbers anywhere from 20,000 items (for a Bubble Sort) to 200 million items (for a Merge Sort). Don't print the large arrays. Rather print date/time stamps before the sorting starts and when the sorting ends. Record benchmark results (hint: do 20000 for the bubble sort, 200 million for the Merge Sort. Use the Arrays.sort() method and benchmark it with 200 million random numbers. Create a built-in sort. Submit your benchmark results.

Merge code:

import java.util.XXXXXXXX; //library for dealing with random nums import java.util.YYYYYYYYY; //library for dealing with dates/times /* Java program for Merge Sort */ class MergeSort_a { // Merges two subarrays of arr[]. // First subarray is arr[l..m] // Second subarray is arr[m+1..r] void merge(int arr[], int l, int m, int r) { // Find sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays */ int L[] = new int[n1]; int R[] = new int[n2]; /*Copy data to temp arrays*/ for (int i = 0; i < n1; ++i) L[i] = arr[l + i]; for (int j = 0; j < n2; ++j) R[j] = arr[m + 1 + j]; /* Merge the temp arrays */ // Initial indexes of first and second subarrays int i = 0, j = 0; // Initial index of merged subarry array int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } /* Copy remaining elements of L[] if any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy remaining elements of R[] if any */ while (j < n2) { arr[k] = R[j]; j++; k++; } } // Main function that sorts arr[l..r] using // merge() void sort(int arr[], int l, int r) { if (l < r) { // Find the middle point int m = (l + r) / 2; // Sort first and second halves sort(arr, l, m); sort(arr, m + 1, r); // Merge the sorted halves merge(arr, l, m, r); } } /* A utility function to print array of size n */ static void printArray(int arr[]) { int n = arr.length; for (int i = 0; i < n; ++i) System.out.print(arr[i] + " "); System.out.println(); } // Driver code public static void main(String args[]) { int arr[] = { 12, 11, 13, 5, 6, 7 }; System.out.println("Given Array"); printArray(arr); //eventually comment out the println statement //and replace it with displaying a time-stamp MergeSort_a ob = new MergeSort_a(); ob.sort(arr, 0, arr.length - 1); System.out.println(" Sorted array"); printArray(arr); //eventually comment out the println statement //and replace it with displaying a time-stamp } } /* This code is contributed by Rajat Mishra */

Bubble Code:

import java.util. // Java program for implementation of Bubble Sort class BubbleSort_a { void bubbleSort(int arr[]) { int n = arr.length; for (int i = 0; i < n-1; i++) for (int j = 0; j < n-i-1; j++) if (arr[j] > arr[j+1]) { // swap arr[j+1] and arr[j] int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } /* Prints the array */ void printArray(int arr[]) { int n = arr.length; for (int i=0; i 

Thank you, good help is appreciated!

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

The Database Experts Guide To SQL

Authors: Frank Lusardi

1st Edition

0070390029, 978-0070390027

More Books

Students also viewed these Databases questions