Question
Hello, I'm having some difficulties with my code and haven't been able to figure out the solution. First off why does my output show the
Hello, I'm having some difficulties with my code and haven't been able to figure out the solution. First off why does my output show the process step by step rather than just showing the sorted array? I have to implement this code into a much larger array of 2000. If it shows such a large array step by step like that it might crash my computer haha. My next issue is that i'm trying to get my code to show the comparisons and exchanges made for each of the cases (best, worst, average). Could anyone help me solve both of these issues? I have a data structures assignment and I have to do a write up about merge sort and what I uncovered but until i get this code fixed I won't be able to. I will show you my methods as well as the main method because it has to be set up a certain way.
public static void main(String[] args) { int[] arrayOne = {1,2,3,4,5,6,7,8,9,10}; int[] arrayTwo = {10,9,8,7,6,5,4,3,2,1}; int[] arrayThree = {1,3,2,5,4,7,9,6,8,10};
System.out.println(" ================ "); System.out.println("Testing Merge sort: ");
System.out.println(" --- BEST CASE --- "); mergeSort(arrayOne); System.out.println(" --- WORST CASE --- "); mergeSort(arrayTwo); System.out.println(" --- AVERAGE CASE --- "); mergeSort(arrayThree); System.out.println(" ================ "); }
//These are the mergeSort methods
public static void mergeSort(int[] array) { int exchanges = 0, comparisons = 0; if (array.length > 1) { // split array into two halves int[] left = leftHalf(array); int[] right = rightHalf(array);
// recursively sort the two halves mergeSort(left); mergeSort(right); // merge the sorted halves into a sorted whole merge(array, left, right); } System.out.println("Shell Sort: Number of Exchanges: " + exchanges); System.out.println("Shell Sort: Number of Comparisons: " + comparisons);
System.out.println("Sorted file: "); for(int i: array) { System.out.print(i + " "); } System.out.println(); }
//Returns the first half of the given array. public static int[] leftHalf(int[] array) { int size1 = array.length / 2; int[] left = new int[size1]; for (int i = 0; i < size1; i++) { left[i] = array[i]; } return left; }
// Returns the second half of the given array. public static int[] rightHalf(int[] array) { int size1 = array.length / 2; int size2 = array.length - size1; int[] right = new int[size2]; for (int i = 0; i < size2; i++) { right[i] = array[i + size1]; } return right; } // Merges the given left and right arrays into the given // result array. public static void merge(int[] result, int[] left, int[] right) { int i1 = 0; // index into left array int i2 = 0; // index into right array int exchanges = 0, comparisons = 0; for (int i = 0; i < result.length; i++) { if (i2 >= right.length || (i1 < left.length && left[i1] <= right[i2])) { result[i] = left[i1]; // take from left i1++; } else { result[i] = right[i2]; // take from right i2++; } } }
Also here is what the output looks like in BlueJ. I don't know how to add a screen shot so I will just copy and paste the window.
Testing Merge sort: --- BEST CASE --- Shell Sort: Number of Exchanges: 0 Shell Sort: Number of Comparisons: 0 Sorted file: 1 Shell Sort: Number of Exchanges: 0 Shell Sort: Number of Comparisons: 0 Sorted file: 2 Shell Sort: Number of Exchanges: 2 Shell Sort: Number of Comparisons: 2 Sorted file: 1 2 Shell Sort: Number of Exchanges: 0 Shell Sort: Number of Comparisons: 0 Sorted file: 3 Shell Sort: Number of Exchanges: 0 Shell Sort: Number of Comparisons: 0 Sorted file: 4 Shell Sort: Number of Exchanges: 0 Shell Sort: Number of Comparisons: 0 Sorted file: 5 Shell Sort: Number of Exchanges: 2 Shell Sort: Number of Comparisons: 2 Sorted file: 4 5 Shell Sort: Number of Exchanges: 2 Shell Sort: Number of Comparisons: 2 Sorted file: 3 4 5
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