Quicksort (Java) causes stack Verflow is in array length> 60k

My code works normally (as far as I know) until my input array size (a.length) is about 62000, at which time I get stackoverflowerror I used to call quicksort twice recursively (less than and greater than pivot q), and then I switched to tail recursion As you can see, I chose pivot as the value at the end of the array I know this is not the best way to choose a pivot, but I still shouldn't see the array size of stackoverflowerrors so small, right? What could have caused this? Thank you in advance! This is my code:

public static void quicksort(int[] a,int p,int r)
    {
        int q;
        while (p < r)
        {
            q = partition(a,p,r);
            quicksort(a,q - 1);
            p = q + 1;
        }
    }

    public static int partition(int[] a,int r)
    {
        int j = p - 1;
        int x = a[r];
        for (int i = p; i < r; i++)
        {
            if (a[i] <= x)
            {
                j++;
                swap(a,i,j);
            }
        }
        j++;
        swap(a,j,r);
        return j;
    }

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

Solution

Worst case input (sort order) enables quick sort Θ (n ^ 2). Partitions always place an element on one side of the partition (cormen et al By randomizing sorting (selecting random pivots), no specific input triggers its worst-case behavior

import java.util.Random;

public class Quicksort
{
     private static Random rand = new Random();

     public static void quicksort(int[] arr,int left,int right)
     {
          if (left < right)
          {
               int pivot = randomizedPartition(arr,left,right);
               quicksort(arr,pivot);
               quicksort(arr,pivot + 1,right);
          }
     }

     private static int randomizedPartition(int[] arr,int right)
     {
          int swapIndex = left + rand.nextInt(right - left) + 1;
          swap(arr,swapIndex);
          return partition(arr,right);
     }

     private static int partition(int[] arr,int right)
     {
          int pivot = arr[left];
          int i = left - 1;
          int j = right + 1;
          while (true)
          {
               do
                    j--;
               while (arr[j] > pivot);

               do
                    i++;
               while (arr[i] < pivot);

               if (i < j)
                    swap(arr,j);
               else
                    return j;
          }
     }

     private static void swap(int[] arr,int j)
     {
          int tmp = arr[i];
          arr[i] = arr[j];
          arr[j] = tmp;
     }

     // Sort 100k elements that are in reversed sorted order
     public static void main(String[] args)
     {
          int arr[] = new int[100000];
          for (int i = 0; i < arr.length; i++)
               arr[i] = arr.length - i;

          System.out.println("First 20 elements");
          System.out.print("Before sort: ");
          for (int i = 0; i < 20; i++)
               System.out.print(arr[i] + " ");
          System.out.println();

          quicksort(arr,arr.length - 1);
          System.out.print("After sort: ");
          for (int i = 0; i < 20; i++)
               System.out.print(arr[i] + " ");
          System.out.println();
     }

}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>