我编写了一个合并排序值数组的方法。方法很完美,但我试图显示已经发生的比较和交换的数量。我的第一个想法是创建静态变量(int比较,int交换)并将它们传递给方法。但是我遇到了从辅助方法返回它们的问题。是否可以在同一方法中返回int []和两个整数?如果没有,我如何确定已经发生的比较和交换次数?
这是我的mergeSort和merge方法:
public static int[] mergeSort(int[] A, int comps, int exchs) {
// Array has only 1 element
if( A.length <= 1 ) {
return A;
}
int midPoint = A.length / 2;
// Initialize left and right arrays.
int[] left = new int[midPoint];
int[] right = new int[A.length - midPoint];
System.arraycopy(A, 0, left, 0, midPoint);
System.arraycopy(A, midPoint, right, 0, A.length - midPoint);
//recursively sort left and right arrays
left = mergeSort(left, comps, exchs);
right = mergeSort(right, comps, exchs);
System.out.println("Comparisons" + comps);
System.out.println("Exchanges" + exchs);
return merge(left, right, comps, exchs);
}
private static int[] merge(int[] left, int[] right, int comps, int exchs){
// Initialize the result array.
int[] res = new int[left.length + right.length];
// Initialize the array indexes.
int leftIndex = 0;
int rightIndex = 0;
int resIndex = 0;
// compare each element and merge results
while(leftIndex < left.length && rightIndex < right.length){
if(left[leftIndex] > right[rightIndex]){
res[resIndex] = right[rightIndex];
exchs++;
rightIndex++;
} else {
res[resIndex] = left[leftIndex];
exchs++;
leftIndex++;
}
comps++;
resIndex++;
}
comps++;
// Append remainder of left array into the result array.
while(leftIndex < left.length){
res[resIndex] = left[leftIndex];
exchs++;
leftIndex++;
resIndex++;
}
comps++;
// Append whatever is left from the right array into the result array.
while(rightIndex < right.length) {
res[resIndex] = right[rightIndex];
exchs++;
rightIndex++;
resIndex++;
}
comps++;
return res; // want to return comparisons and exchanges to mergeSort method
}
答案 0 :(得分:2)
创建一个为您排序的对象。然后它可以存储comps
和exchs
,你可以使用getter方法访问它们......
public class MergeSorter {
private int comps = 0;
private int exchs = 0;
public int[] mergeSort(int[] A) {
comps = 0;
exchs = 0;
// your code
}
private int[] merge(int[] left, int[] right) {
// your code
}
public int getLastComps() { return comps; }
public int getLastExchs() { return exchs; }
}
答案 1 :(得分:1)
是否可以在同一方法中返回int []和两个整数?
不直接。但是,您可以创建一个类,该类将封装从方法返回所需的所有内容,并返回该类的实例。
答案 2 :(得分:1)
如果你有一个包含多个合并方法的类,我会创建两个静态类变量。调用方法时,比较和交换都将设置为0.随着代码的进行,这两个值将被更新。因此,在调用其中一个方法后,您将始终知道两个变量都显示正确的值。例如,
private static int comparisons;
private static int exchanges;
public static int[] mergeSort(int[] A, int comps, int exchs) {
comparisons = 0;
exchanges = 0;
if( A.length <= 1 ) {
return A;
}
int midPoint = A.length / 2;
int[] left = new int[midPoint];
int[] right = new int[A.length - midPoint];
System.arraycopy(A, 0, left, 0, midPoint);
System.arraycopy(A, midPoint, right, 0, A.length - midPoint);
//recursively sort left and right arrays
left = mergeSort(left, comps, exchs);
right = mergeSort(right, comps, exchs);
comparisons = comps;
exchanges = exchs;
return merge(left, right, comps, exchs);
}
现在,您可以在调用方法后访问这些静态变量,并且只要再次调用该方法,就会刷新这些静态变量。
答案 3 :(得分:0)
您必须将这些值包装在一起。因为您无法一次从方法返回多个值。所以将它们全部放在一个对象上并返回该对象。
示例:
class WrapperClass {
int[] array;
int valueOne;
int valueTwo;
// Setters and Getters to these members.
}
样本方法。
public WrapperClass method() {
// Set your values to the object;
WrapperClass wrapObj = new WrapperClass();
// set array and the two values to this object.
// return the object.
return wrapObj;
}