简体   繁体   English

java mergesort计数作业

[英]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): 这样的事情(可能在您的main方法中):

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; 运行Java类时,它将寻找一个public static void main(String[] args)方法; 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); 现在,您实际上应该从System.out.println(howMany);获得编译错误System.out.println(howMany); since the variable howMany only exists within the scope (brackets) of the merge method. 因为变量howMany仅存在于merge方法的范围(括号)内。 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 您还需要使mergeSort方法为static方法,因此将其定义更改为

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! 最好的运气学习面向对象编程!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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