Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I want you to arrange the code better and use a short and understandable comment. Use a style code to make the code more organized
I want you to arrange the code better and use a short and understandable comment. Use a style code to make the code more organized and understandable
package Checkpoint; /** * A class to implement a set of sorting algorithms. * */ public class Sorts implements Sorting{ @Override public void Sorting() { } @Override /** * Sort the array of strings, v, into ascending lexicographic order using Insertion sort. * No errors may be reported and no exceptions may be thrown. * If an error is detected or an exception occurs, return without modifying the array. * * @param a The array to be sorted. */ public void insertionSort(String[] a) { for (int j = 1; j < a.length; ++j) { // asume index 0 element is sorted then start from index 1.. String key = a[j];// Assigning current value that the value pointed by j . int i = j - 1; // take value of i from j-1 as the left part of j is always sorted according while ((i >= 0) && (a[i].compareTo(key) > 0)) { // use comparesTo for compare two strings // lexicographically... a[i + 1] = a[i];// if the condition is true ,assigning the value present at index i to index i+1 i = i - 1; // reducing the value of i by one a[i + 1] = key; // replacing the value present at index i+1 by the value at key } } } @Override /** * Sort the array of {@link strings}, v, into ascending lexicographic order using Merge sort. * No errors may be reported and no exceptions may be thrown. * If an error is detected or an exception occurs, return without modifying the array. * * @param a The array to be sorted. * @param p The left index of the array. * @param r The right index of the array. */ public void mergeSort(String[] a, int p, int r) { if (p < r) { // q is the mid of array that is dividing the array a in 2 equal subarray int q = (p + r) / 2; // floor.. mergeSort(a, p, q); mergeSort(a, q + 1, r); merge(a, p, q, r);// This merge to already sorted array } } void merge(String array[], int p, int q, int r) { // Merge two sorted array // sizes of two subarrays to be merged int n1 = q - p + 1; int n2 = r - q; // Create temp arrays String L[] = new String[n1]; String R[] = new String[n2]; // Copy data to temp arrays for (int i = 0; i < n1; ++i) { L[i] = array[p + i]; } for (int j = 0; j < n2; ++j) { R[j] = array[q + 1 + j]; } // Initial indexes of first and second subarrays int i = 0, j = 0; /* * We will loop through both array simultaniously untill we reach the and of any * one subarray */ int k = p; while (i < n1 && j < n2) { if (L[i].compareTo(R[j]) <= 0) { array[k] = L[i]; i++; } else { array[k] = R[j]; j++; } k++; } // if we had not reached the end of subarray 1 we will put all the elements of // subarray 1 . // Copy remaining elements of L[] if any. while (i < n1) { array[k] = L[i]; i++; k++; } // if we had not reached the end of subarray 2 we will put all the elements of // subarray 2 // Copy remaining elements of R[] if any while (j < n2) { array[k] = R[j]; j++; k++; } } } package Checkpoint; import java.util.Arrays; import java.util.Random; /** * * The class acts as a driver to test your implemented sorting algorithms */ public class Checkpoint2Driver { public static void main(String[] args) { // Test your code here .... System.out.println("***************************"); System.out.printf("Insertion sort result "); System.out.printf("Array Size t Excecution Time "); int n = 1000; double exTime = efficiencyTesting(generateRandomArray(n)); System.out.printf("%10dt%f ", n, exTime); n = 5000; exTime = efficiencyTesting(generateRandomArray(n)); System.out.printf("%10dt%f ", n, exTime); n = 10000; exTime = efficiencyTesting(generateRandomArray(n)); System.out.printf("%10dt%f ", n, exTime); n = 50000; exTime = efficiencyTesting(generateRandomArray(n)); System.out.printf("%10dt%f ", n, exTime); n = 75000; exTime = efficiencyTesting(generateRandomArray(n)); System.out.printf("%10dt%f ", n, exTime); n = 100000; exTime = efficiencyTesting(generateRandomArray(n)); System.out.printf("%10dt%f ", n, exTime); System.out.println("************************"); System.out.printf("Merge sort result "); System.out.printf("Array Size t Excecution Time "); n = 1000; exTime = efficiencyMergeTesting(generateRandomArray(n)); System.out.printf("%10dt%f ", n, exTime); n = 5000; exTime = efficiencyMergeTesting(generateRandomArray(n)); System.out.printf("%10dt%f ", n, exTime); n = 10000; exTime = efficiencyMergeTesting(generateRandomArray(n)); System.out.printf("%10dt%f ", n, exTime); n = 50000; exTime = efficiencyMergeTesting(generateRandomArray(n)); System.out.printf("%10dt%f ", n, exTime); n = 75000; exTime = efficiencyMergeTesting(generateRandomArray(n)); System.out.printf("%10dt%f ", n, exTime); n = 100000; exTime = efficiencyMergeTesting(generateRandomArray(n)); System.out.printf("%10dt%f ", n, exTime); } public static int testingOutcoms (String text, String[] unsorted_test_1, String[] sorted_test_1) { if(Arrays.equals(unsorted_test_1, sorted_test_1)) { System.out.println(text + " Test Pass"); return 1; } else { System.out.println(text + " Test Fail"); return 0; } } /** * Generates an array of {@link String}randomly. * @param size - The size of the array to be generated. * * @return A randomly generated array. */ public static String[] generateRandomArray (int size) { Random randomGenerator = new Random(); String [] array = new String [size]; for(int i=0;i
Step by Step Solution
★★★★★
3.33 Rating (147 Votes )
There are 3 Steps involved in it
Step: 1
Java package Checkpoint A class to implement a set of sorting algorithms public class Sorts implemen...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