Question
I'm making a mergesort method and when I output the results come out weird package mergesort; public class MergeSort { /* Precondition: Every indexed variable
I'm making a mergesort method and when I output the results come out weird
package mergesort;
public class MergeSort { /* Precondition: Every indexed variable of the array a has a value. Postcondition: a[0] <= a[1] <=.. <= a[a.length - 1]. */ public static void sort(int[] a) { if (a.length >= 2) { int halfLength = a.length / 2; int [] firstHalf = new int[halfLength]; int [] lastHalf = new int[a.length - halfLength]; divide(a, firstHalf, lastHalf); sort(firstHalf); sort(lastHalf); merge(a, firstHalf, lastHalf); } //Else do nothing. a.length == 1, so a is sorted } /*Precondition: a.length = firstHalf.length + lasHalf.length Postcondition: All the elements of a are divided between the arrays of firstHalf and lastHalf. */ private static void divide(int[] a, int[] firstHalf, int[] lastHalf) { for (int i = 0; i < firstHalf.length; i++) firstHalf[i] = a[i]; for (int i = 0; i < lastHalf.length; i++) lastHalf[i] = a[firstHalf.length + i]; } /*Precondition: Arrays firstHalf and lastHalf are sorted from smallest to largest; a.length = firstHalf.length + lastHalf.length Postcondition: Array a contains all the values from firstHalf and lastHalf and is sorted from smallest to largest. */ private static void merge(int[] a, int[] firstHalf, int[] lastHalf) { int firstHalfIndex = 0, lastHalfIndex = 0, aIndex = 0; while ((firstHalfIndex < firstHalf.length) && (lastHalfIndex < lastHalf.length)) { if (firstHalf[firstHalfIndex] < lastHalf[lastHalfIndex]) { a[aIndex] = firstHalf[firstHalfIndex]; firstHalfIndex++; } else { a[aIndex] = firstHalf[firstHalfIndex]; lastHalfIndex++; } aIndex++; } /*At least one of firstHalf and lastHalf has been completely copied to a. */ //Copy rest of firstHalf,if any. while (firstHalfIndex < firstHalf.length) { a[aIndex] = firstHalf[firstHalfIndex]; aIndex++; firstHalfIndex++; } //Copy rest of lastHalf, if any. while (lastHalfIndex < lastHalf.length) { a[aIndex] = lastHalf[lastHalfIndex]; aIndex++; lastHalfIndex++; } } }
package mergesort;
public class MergeSortDemo { public static void main(String[] args) { int[] anArray = {7, 5, 11, 2, 16, 4, 18, 14, 12, 30}; System.out.println("Array values before sorting;"); for (int i = 0; i < anArray.length; i++) System.out.print(anArray[i] + " "); System.out.println(); MergeSort.sort(anArray); System.out.println("Array values after sorting:"); for (int i = 0; i < anArray.length; i++) System.out.print(anArray[i] + " "); System.out.println(); } }
When I try to run the demo I get this output:
Array values before sorting; 7 5 11 2 16 4 18 14 12 30 Array values after sorting: 7 7 7 11 11 16 18 18 18 30
i can't figure out where my problem is.
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