简体   繁体   中英

Why do I get a stack overflow error?

Here is the code .. I have to sort an already sorted array and to calculate it's execution time ...for quicksort it is n^2 cause it is the worst case. but for large input data let's say 7500 it gives me an overflow error :S what can i do in order to calculate the running time?

public class Provo {

    public static void swap(int[] a, int i, int j) {
        int temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }

    public static int HoarePartition(int m, int d, int[] a) {
        int pivot = a[m];
        int i = m + 1;
        int j = d;

        while (i < j) {
            while (a[i] < pivot) {
                i = i + 1;
            }

            while (a[j] > pivot) {
                j = j - 1;
            }

            if (i < j)
                swap(a, i, j);
        }
        swap(a, m, j);

        return j;
    }

    public static void quicksort(int m, int d, int[] a) {
        if (m < d) {
            int s = HoarePartition(m, d, a);
            quicksort(m, s - 1, a);
            quicksort(s + 1, d, a);
        }
    }
}

and here is the main class

import javax.swing.*;

public class ascending {


public static void main(String[] args){
    String input=JOptionPane.showInputDialog("Shkruani nr e te dhenave");
    int size=new Integer(input).intValue();

    int[] r= new int[size];
    int[] p = new int[size];
    int majtas=0;
    int djathtas=size;

    for(int i=majtas;i<djathtas;i++)
    {r[i]=i;}
    for(int i=majtas;i<djathtas;i++)
    {p[i]=r[i];}

    long average;
    int n=100;
    long result=0;
    for(int j=1;j<=n;j++) 
    {
        long startTime = System.nanoTime();
        Provo.quicksort(majtas,djathtas-1,p);
     long endTime = System.nanoTime();
     result = result+(endTime-startTime);
   long a = endTime-startTime;
        System.out.println(j+": " +a);
        for(int i=majtas;i<djathtas;i++)
        {p[i]=r[i];}
    }
    average=result/n;
    System.out.println("Koha e ekzekutimit te insertion sort eshte " + average + " nanosekonda ");
}

}

Well if you are stuck, you can maybe look for iterative quicksort on the web to get some help.

I have found this article . It is dedicated to C# but translating it into Java shouldn't be a big issue.

Change int djathtas = size - 1; to int djathtas = size; and change quicksort(majtas, djathtas, p); to quicksort(majtas, djathtas - 1, p);

Otherwise it's not allocating 10 digits, only 9.

It seems like you're just using too large of a number. Your program can be functioning correctly and generate a stack overflow error. You could try implementing another version of quicksort that does not rely on the stack or something.

Other than that, I'm not sure why you need such a large input anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM