Results 1 to 2 of 2
  1. #1
    erkidhoxholli is offline Member
    Join Date
    Jan 2014
    Posts
    1
    Rep Power
    0

    Exclamation Java QuickSort StackOverflowError

    Hello.I have an error using quicksort and this is a project I have to deliver tomorrow so I hope someone is going to answer tonight.The error occurs for numbers such as 7500 and bigger . Any help ? Thanks!

    Exception in thread "main" java.lang.StackOverflowError
    at QuickSort.QuickSort(QuickSort.java:45)
    at QuickSort.QuickSort(QuickSort.java:46)
    at QuickSort.QuickSort(QuickSort.java:46) ...
    P.S : SORRY LANGUAGE IS IN ALBANIAN !

    Java Code:
    import javax.swing.JOptionPane;
    public class QuickSort{
        public static void main(String[] args){
            int p=new Integer(JOptionPane.showInputDialog("Jepni numrin e kufizave: ")); 
            int[] ListaNumrave= new int[p];
            //QuickSort Zbrites
            listaZbrites(p, ListaNumrave);
            long nisKohaZ = System.currentTimeMillis();
            QuickSort(1, p-1, ListaNumrave);
            System.out.println("Kohezgjatja e numrave ne Rend Zbrites me metoden QuickSort per p = " + p + " eshte : " + (System.currentTimeMillis() - nisKohaZ) + " milisekonda"  );
            //QuickSort Rastit
            listaRastit(p, ListaNumrave);
            long startTipi2QS = System.currentTimeMillis();
            QuickSort(0, p-1, ListaNumrave);
            System.out.println("Kohezgjatja e numrave te Rastit me metoden QuickSort per p =  " + p + " eshte : " + (System.currentTimeMillis() - startTipi2QS) + " milisekonda"  );
            //QuickSort Rrites
            listaRrites(p, ListaNumrave);
            long startTipi3QS = System.currentTimeMillis();
            QuickSort(0, p-1, ListaNumrave);
            System.out.println("Kohezgjatja e numrave ne Rend Rrites me metoden QuickSort per p = " + p + " eshte : " + (System.currentTimeMillis() - startTipi3QS) + " milisekonda"  );
        }
        // Tabela me elemente te renditur ne rendin zbrites.
        static void listaZbrites(int n, int[] list){
            list[0]=n;
            for(int i=1; i<list.length; i++ ){
                list[i] = list[i-1]-1;}
            }
        // Tabela me elemte te gjeneruar ne menyre te rastit.
        static void listaRastit(int n, int[] list){
            for(int i=0; i<list.length; i++ ){
                list[i]= 1+ (int) (Math.random()*((n-1)+1));
            }
        }
        // Tabela me elemente te renditur ne rendin rrites.
        static void listaRrites(int n, int[] list){
            list[0]=1;
            for(int i=1; i<list.length; i++ ){
                list[i] = list[i-1]+1;
            }
        }
       
        // Algoritmi i renditjes Quick Sort
        static void QuickSort(int majtas, int djathtas,int[] list){
            if(majtas<=djathtas){
                int s= Ndarje(majtas, djathtas, list);
            QuickSort(majtas, s-1, list);   //Ndarja e majte 
            QuickSort(s+1, djathtas, list);   //Ndarja e djathte
            }
        }
    
        static int Ndarje(int majtas, int djathtas, int[] list){
            int i=majtas+1,j=djathtas; 
            while( i<=j ){
                while( (i<=djathtas) && (list[i]<=list[djathtas]) ) i++; 
                while( list[j]>list[majtas] ) j--; 
                if( i<=j ){
                    // Ndodh perkembimi midis list[i] dhe list[j].
                    // Anullon perkembimin e fundit kur i >=j.
                    int temporal=list[i]; 
                    list[i]=list[j]; 
                    list[j]=temporal;
                }
            } 
    
        // Ndodh perkembimi midis list[majtas] dhe list[j].
            int temporal=list[majtas]; 
            list[majtas]=list[j]; 
            list[j]=temporal; 
            return j ; 
        }
    }

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Java QuickSort StackOverflowError

    You can recurs only "so deep", and you seem to have hit that limit.

Similar Threads

  1. How to limit threads in a Java QuickSort algorithm.
    By roise_r in forum Threads and Synchronization
    Replies: 6
    Last Post: 06-02-2011, 07:53 PM
  2. java.lang.StackOverflowError???
    By remus19 in forum New To Java
    Replies: 6
    Last Post: 05-06-2011, 02:55 AM
  3. java.lang.StackOverflowError
    By malstryx in forum New To Java
    Replies: 9
    Last Post: 10-01-2008, 04:14 AM
  4. java.lang.StackoverflowError
    By ravisankarvivek in forum New To Java
    Replies: 6
    Last Post: 06-23-2008, 09:05 AM
  5. java.lang.StackOverflowError
    By eva in forum New To Java
    Replies: 3
    Last Post: 12-24-2007, 09:54 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •