Java 在merge方法中返回多个变量
我已经写了一个方法来合并排序数组的值。这个方法非常有效,但我试图展示已经发生的比较和交换的数量。我的第一个想法是创建静态变量(int比较、int交换)并将它们传递到方法中。但是,我从helper方法返回它们时遇到了问题。是否可以在同一方法中返回一个int[]和两个int?如果没有,我如何确定已经进行的比较和交流的数量 以下是我的合并排序和合并方法:Java 在merge方法中返回多个变量,java,sorting,recursion,Java,Sorting,Recursion,我已经写了一个方法来合并排序数组的值。这个方法非常有效,但我试图展示已经发生的比较和交换的数量。我的第一个想法是创建静态变量(int比较、int交换)并将它们传递到方法中。但是,我从helper方法返回它们时遇到了问题。是否可以在同一方法中返回一个int[]和两个int?如果没有,我如何确定已经进行的比较和交流的数量 以下是我的合并排序和合并方法: public static int[] mergeSort(int[] A, int comps, int exchs) { //
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
}
公共静态int[]合并排序(int[]A、int comps、int exchs){
//数组只有1个元素
if(A.右长[rightIndex]){
res[resIndex]=右[rightIndex];
exchs++;
rightIndex++;
}否则{
res[resIndex]=左[leftIndex];
exchs++;
leftIndex++;
}
comps++;
resIndex++;
}
comps++;
//将左数组的剩余部分追加到结果数组中。
while(leftIndex
是否可以在同一方法中返回一个int[]和两个int
不直接。但是,您可以创建一个类来封装从方法返回的所有内容,并返回该类的实例
是否可以在同一方法中返回一个int[]和两个int
不直接。但是,您可以创建一个类来封装从方法返回的所有内容,并返回该类的一个实例。创建一个为您进行排序的对象。然后它可以存储
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; }
}
创建一个为您进行排序的对象。然后它可以存储
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; }
}
如果您有一个具有多个合并方法的类,我将创建两个静态类变量。调用方法时,比较和交换都将设置为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);
}
私有静态int比较;
私人静态交换;
公共静态int[]合并排序(int[]A,int comps,int exchs){
比较=0;
交换=0;
如果(A.length如果您有一个具有多个合并方法的类,我将创建两个静态类变量。当调用一个方法时,比较和交换都将设置为0。随着代码的进行,这两个值将被更新。因此,在调用其中一个方法后,您将始终知道,两个变量都显示了正确的va吕,比如说,
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);
}
私有静态int比较;
私人静态交换;
公共静态int[]合并排序(int[]A,int comps,int exchs){
比较=0;
交换=0;
如果(A.length您必须将这些值包装在一起。因为您不能从一个方法一次返回多个值。因此,请将它们全部放在一个对象中并返回该对象
例如:
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;
}
您必须将这些值包装在一起。因为您不能从一个方法一次返回多个值。因此,请将所有值放在一个对象中,然后返回该对象
例如:
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;
}
你可以修改变量输入(交换,res),然后查阅它们…你可以修改变量输入(交换,res),然后查阅它们…这与我的想法非常相似。我的排序方法都包含在“分类器”中类,因此这非常便于实现。非常感谢!这与我的想法非常相似。我的排序方法都包含在“Sorter”类中,因此这非常便于实现。非常感谢!