简体   繁体   中英

java mergesort count homework

I am working on getting the counts of comparisons and movers when merge sorting. I think I have the recursion I need thanks to this Sort Comparisons Counter but I can not get it to print out. I am obviously very new at programming so I'd be appreciative if you could explain what it is that I am missing.

import java.util.Arrays;


public class MergeSort {
int count = 0;
/**
 * @param args
 */

// Rearranges the elements of a into sorted order using
// the merge sort algorithm (recursive).
public int mergeSort(int[] a, int howMany) {


    if (a.length >= 2) {
        // split array into two halves
        int[] left  = Arrays.copyOfRange(a, 0, a.length/2);
        int[] right = Arrays.copyOfRange(a, a.length/2, a.length);


        // sort the two halves
       howMany = mergeSort(left,howMany);
       howMany = mergeSort(right, howMany);

        // merge the sorted halves into a sorted whole
       howMany = merge ( left, right, a, howMany);


    }

   return howMany;
}




// Merges the left/right elements into a sorted result.
// Precondition: left/right are sorted
public static int merge(int[] result, int[] left, 
                                       int[] right, int howMany) {
    int i1 = 0;   // index into left array
    int i2 = 0;   // index into right array

    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++;
        }

    }

    return howMany;
}

System.out.println(howMany); // ???
}

you need to call the method through its object wherever you wanna print. something like this (may be in your main method):

MergeSort mObj - new MergeSort();
int[] array = {1,2,3};
int count =  mObj.mergeSort(array, 2);
System.out.println(count);

Basically, you need a driver method. When a java class is run, it will look for a public static void main(String[] args) method; if this method doesn't exist, nothing will happen. With what you have now, you should actually get a compile error from System.out.println(howMany); since the variable howMany only exists within the scope (brackets) of the merge method. To understand this better I'd review your notes on variable and method scope and class members. For a quick fix, remove the line at the bottom that I mentioned above, and place this method somewhere in your class:

public static void main(String[] args) {
    int[] array = {2,5,8,1,3};
    int howMany = mergeSort(array, 5);
    System.out.println(howMany);
}

You also need to make your mergeSort method static , so change its definition to

public **static** int mergeSort(int[] a, int howMany)

I tested your code and I'm pretty sure that it doesn't give the answer you want, so be sure to check that. Best of luck learning object oriented programming!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM