簡體   English   中英

我的快速排序方法有一個基本案例,但我仍然收到堆棧溢出錯誤?

[英]I have a base case for my quick sort method but I still get a stack overflow error?

我有一個快速排序類,它適用於較小的列表大小,但即使我有一個基本案例,也會在較大的大小上不斷出現 SO 錯誤。 我有一個快速排序類,如下所示:

public class QuickSorter extends Sorter
{
  @Override
  public void sort(WordList toSort, Comparator<String> comp) throws NullPointerException{
    // TODO
      int front = 0;
      
      int back = toSort.length() -1;
      
      quickSortRec(toSort, comp, front, back);
      
  }

  private void quickSortRec(WordList list, Comparator<String> comp, int start, int end){
    // TODO
      if (start >= end) {
          return;
      }
    
      int pivotPoint = partition(list, comp, start, end);
         
      quickSortRec(list, comp, start, pivotPoint - 1);
     
      quickSortRec(list, comp, pivotPoint + 1, end);
     
      }
    
  
     
  
         
  

  private int partition(WordList list, Comparator<String> comp, int start, int end){
    // TODO
      String pivotSpot = list.get(end);
      int pivotIndex = start;
     
      for(int i = start; i < end; i++) {
          
          if(comp.compare(list.get(i), pivotSpot) < 0) {
              
              list.swap(i, pivotIndex); 
          }
      }
      
      list.swap(end, pivotIndex);
     
      return pivotIndex;
  
  
  }
}

我的代碼在需要排序的較小列表上運行良好,但在第 36 行出現重復的 StackOverflow 異常

堆棧跟蹤如下所示:

Exception in thread "main" java.lang.StackOverflowError
    at hw2.AlphabetComparator.compare(AlphabetComparator.java:1)
    at hw2.Sorter$CountingComparator.compare(Sorter.java:272)
    at hw2.QuickSorter.partition(QuickSorter.java:53)
    at hw2.QuickSorter.quickSortRec(QuickSorter.java:32)
    at hw2.QuickSorter.quickSortRec(QuickSorter.java:36)
    at hw2.QuickSorter.quickSortRec(QuickSorter.java:36)
    at hw2.QuickSorter.quickSortRec(QuickSorter.java:36)
    at hw2.QuickSorter.quickSortRec(QuickSorter.java:36)

字母比較器:

int length = b.length(); // holds smallest length of both strings so I don't get an out of bounds exception with my for loop

      if (a.length() < b.length()) // default length will be String b's length if b is less than a or a and b are ==
      {
          length = a.length();
      }
      for (int i = 0; i < length; i++)
      {
          if (a.charAt(i) != b.charAt(i)) // if character at same index in both strings aren't equal
          {
              if (alphabet.isValid(a.charAt(i)) == true && alphabet.isValid(b.charAt(i)) == true) // if both characters are valid in the alphabet
              {
                  return alphabet.getPosition(a.charAt(i)) - alphabet.getPosition(b.charAt(i)); // return negative or positive
              }
          }
      }
      if (a.length() != b.length())
      {
          if (length == a.length())
          {
              return -1;
          } else
              return 1;
      }
      return 0;
  }
      ```

您正在使用遞歸進行排序,並且堆棧不是無限的。 對於小(足夠)的數組,您的遞歸保持在限制內。 但是對於足夠大的陣列,它會炸毀堆棧。 通常,堆棧溢出發生在無限循環場景中,但在您的情況下,由於數組太寬,您可能只是遞歸得太深了。

參考 https://freecontent.manning.com/stack-safe-recursion-in-java/#:~:text=Default%20stack%20size%20varies%20between,case%20is%20recursive%20method%20calls

您的下一個問題可能在這里得到解答: 如何增加 Java 堆棧大小?

暫無
暫無

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

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