编码堆栈算法,但我得到一个堆栈溢出错误,无法弄清楚原因

时间:2018-03-03 21:49:53

标签: java sorting stack-overflow heapsort

下面是我的代码,我不断从我的代码中的最后一个语句得到Stack Overflow错误,这是对heapify(max heapify)方法的递归调用。请帮忙。

1级代码

package hw3javasorttest;    
import java.util.*;    
public class HW3JavaSortTest {

    /**
     * @param args the command line arguments
     */
     public static void main(String[] args) {

        System.out.println("Unsorted array");
        System.out.print("[");
        int[] arr = new int[30];
        for(int i = 0; i <  arr.length; i++) {
            arr[i] = (int)(Math.random() * 100);
            System.out.print(arr[i] + "  ");
        }
        System.out.println("]");

        Scanner in = new Scanner(System.in);
        System.out.println("Please enter one of the corresponding numbers to choose the sorting method: ");
            System.out.print("1.Heap Sort\n2.Quick Sort");

            int Sorting = in.nextInt();
            int x;
            int q;
            x = arr[0];
            q = arr[29];


        switch (Sorting) {
                case 1:  System.out.println("Heap Sort:");

                         HW3JavaSort.HeapSort(arr);
                         System.out.println(Arrays.toString(arr));
                         break;
                case 2:   System.out.println("Quick Sort:");
                          System.out.println();
                         // HW3JavaSort.quickSort(arr, x, q);
                         // System.out.println(Arrays.toString(a));
                         break;
                default: System.out.println("invalid entry");
                         break;
            }
    }
}

第2类代码

package hw3javasorttest;


public class HW3JavaSort {

    public static void printArr(int[] arr) {  //Method that displays arr 
       System.out.print("[");
        for(int i= 0; i<arr.length; i++){

            if(i==arr.length-1) {
                System.out.printf("%d]\n", arr[i]);
            }
            else {
               System.out.printf("%d,", arr[i]); 
            }
    }
}
    public static void HeapSort(int[] arr) {
         int Length = arr.length;
         int placeholder;
         BuildMaxHeap(arr, Length);
         for(int i = arr.length-1; i>0; i--) {
             placeholder = arr[0];
             arr[0] = arr[i];
             arr[i] = placeholder;
             heapify(arr, 1, i);
        }
    } 
    public static void BuildMaxHeap(int[] arr, int n){ //Organizes max heap
         if(arr == null) {
            throw new NullPointerException("null");
        }
        if(arr.length <=0 || n <= 0) {
            throw new IllegalArgumentException("illegal");
        }
        if(n > arr.length) {
            n = arr.length;
        }

        for(int i = n/2; i>= 0; i--) {
            heapify(arr, i, n);
        }
    }
    public static void heapify(int [] arr, int i, int n) { //Makes max heap
        int largest;
        int lc = 2*i;
        int rc = 2*i + 1;
        int temp = 0;

        if(lc<=n && arr[lc-1] > arr[i-1]) {
            largest = lc;
        } else {
            largest = i;
        }
        if(rc<=n && arr[rc-1] > arr[largest-1]) {
            largest = rc;
        }

        if(largest!=i) {
            temp = arr[i-1];
            arr[i-1] = arr[largest - 1];
            arr[largest - 1] = temp;
            heapify(arr, largest, n); //HERE IS WHERE THE COMPILER SAYS I AM //GETTING THE ERROR, SAYS STACK OVERFLOW THEN THE HEAPIFY METHOD THEN THIS LINE //# AND DISPLAYS THE ERROR HUNDREDS OF TIMES
        }
    }
}

1 个答案:

答案 0 :(得分:0)

不正确的格式化导致算法的工作方式与预期不同...由Java(不是Python)等格式化修复。

请看一下:https://blog.takipi.com/tabs-vs-spaces-how-they-write-java-in-google-twitter-mozilla-and-pied-piper/

相关问题