簡體   English   中英

嘗試在 Java 中編碼合並排序時出現 StackOverFlow 錯誤

[英]Getting StackOverFlow error while trying to code Merge Sort in Java

我是編碼新手,我試圖在 java 中創建合並排序算法。 我收到太多錯誤,無法找出代碼中的確切錯誤。 我覺得我的邏輯是正確的,但不知道是哪個步驟導致了錯誤。 有人可以幫我糾正以下代碼中的錯誤。 謝謝

package com.company;
public class MergeSort_Array {
    //Method created to print Input Array
    public static void printInputArray(int inputArray[]) {
        for (int i:inputArray) { //for-each loop
            System.out.print(i + " ");
        }
        System.out.println();
    }

    //Function created to sort and merge Input Array:
    public static void SortArray(int[] A) {
        int midpoint = A.length / 2;
        int[] left = new int[midpoint];
        int[] right;
        if (A.length % 2 == 0) {
            right = new int[midpoint];
        } else {
            right = new int[midpoint + 1];
        }
        //Copying values from super Array to left Array:
        for (int i = 0; i < midpoint; i++) {
            left[i] = A[i];
        }
        //Copying elements from super Array to right Array:
        for (int j = 0; j < right.length; j++) {
            right[j] = A[midpoint + j];
        }
        //using Recursion
        SortArray(left);
        SortArray(right);
        MergeArray(A, left, right);
    }

    // Creating a Function to merge left and right arrays.
    public static void MergeArray(int[] result, int[] L, int[] R) {
        //result array length = length of left array+ right array length
        result = new int[L.length + R.length];
        int i = 0, j = 0, k = 0;
        while (k < result.length) {
            if (L[i] < R[j]) {
                result[k] = L[i];
                i++;
            } else
            if (R[j] < L[i]) {
                result[k] = R[j];
                j++;
            } else
            if (i > L.length) {
                while (j <= R.length) {
                    result[k] = R[j];
                    j++;
                }
            } else
            if (j > R.length && i <= L.length) {
                while (i <= L.length) {
                    result[k] = L[i];
                    i++;
                }
            }
            k++;
        }
    }

    public static void main(String[] args) {
        int[] inputArray = { 2, 5, 4, 1, 7, 9, 6 };
        MergeSort_Array ms = new MergeSort_Array();
        ms.printInputArray(inputArray);
        SortArray(inputArray);

        for (int i: inputArray) {
            System.out.println(i + " ");
        }
    }
}

每次調用SortArray時,它本身都會調用SortArray兩次。 沒有結束條件:每次調用都會嘗試調用SortArray兩次。

這意味着無法完成對SortArray的調用,因為您在每個調用中都無限遞歸。

必須有一些基本情況,它不再調用自己。 對於合並排序,一旦數組足夠小,通常會切換到其他算法,但為了簡單起見,您甚至可以退回到最簡單的排序基本情況:任何短於 2 個元素的數組總是被排序,不需要做任何其他事情進行排序。

您的代碼中有多個問題:

  • [主要] SortArray()總是嘗試拆分數組並對兩半進行排序。 如果數組長度小於 2,則不應這樣做,否則會導致無限遞歸,從而導致堆棧溢出異常。

  • [Hint] right可以無條件初始化為int[] right = new int[A.length - midpoint];

  • [主要] MergeArray不應重新分配目標數組。 必須在result中執行合並,以便更新調用者的 object。

  • [主要]在合並循環中,您必須在嘗試讀取L[i]R[j]之前測試索引值,否則您可能會遇到越界異常。

這是修改后的版本:

package com.company;
public class MergeSort_Array {
    // Method created to print Input Array
    public static void printInputArray(int inputArray[]) {
        for (int i : inputArray) { //for-each loop
            System.out.print(i + " ");
        }
        System.out.println();
    }

    //Function created to sort and merge Input Array:
    public static void SortArray(int[] A) {
        if (A.length >= 2) {
            int midpoint = A.length / 2;
            int[] left = new int[midpoint];
            int[] right = new int[A.length - midpoint];

            //Copying values from super Array to left Array:
            for (int i = 0; i < midpoint; i++) {
                left[i] = A[i];
            }
            //Copying elements from super Array to right Array:
            for (int j = 0; j < right.length; j++) {
                right[j] = A[midpoint + j];
            }
            //using Recursion
            SortArray(left);
            SortArray(right);
            MergeArray(A, left, right);
        }
    }

    // Creating a Function to merge left and right arrays.
    public static void MergeArray(int[] result, int[] L, int[] R) {
        for (int i = 0, j = 0, k = 0; k < result.length; k++) {
            if (j >= R.length || (i < L.length && L[i] < R[j])) {
                result[k] = L[i++];
            } else {
                result[k] = R[j++];
            }
        }
    }

    public static void main(String[] args) {
        int[] inputArray = { 2, 5, 4, 1, 7, 9, 6 };
        printInputArray(inputArray);
        SortArray(inputArray);
        printInputArray(inputArray);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM