2012-12-05 53 views
0

我寫了一個方法來合併排序值的數組。方法完美的工作,但我試圖展示已發生的比較和交流的數量。我的第一個想法是創建靜態變量(int比較,int交換)並將它們傳遞給方法。但是遇到從輔助方法返回它們的問題。是否有可能在同一個方法中返回一個int []和兩個整數?如果不是,我如何確定已經發生的比較和交換次數?在合併方法中返回多個變量

這裏是我的歸併和合並方法:

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

創建一個對象,爲您排序。然後,它可以存儲compsexchs,你可以直接訪問它們具有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

這與我的想法非常相似。我的排序方法都包含在「排序器」類中,所以這非常方便實現。非常感謝! –

1

是否有可能在同一個方法返回一個int []和兩個整數?

不直接。但是,您可以創建一個類來封裝您從方法返回的所有內容,並返回該類的一個實例。

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

您現在可以訪問這些靜態變量的方法被調用後,每當該方法再次被調用將被刷新。

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