Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6737001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:11:48+00:00 2026-05-26T11:11:48+00:00

My code works properly (to my knowledge) up until my input array size (

  • 0

My code works properly (to my knowledge) up until my input array size (a.length) is around 62,000 at which time I consistently get a StackOverFlowError. I previously had used 2 recursive calls to quicksort (less than, and greater than the pivot q) and then I switched to tail recursion. As you can see, I’m selecting the pivot to be the value at the end of the array. I know this isn’t the best way to choose a pivot, but I still shouldn’t be seeing StackOverFlowErrors with an array size this small, right? What could be causing this? Thanks in advance! Here’s my code:

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

    public static int partition(int[] a, int p, 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;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:11:49+00:00Added an answer on May 26, 2026 at 11:11 am

    The worst-case input (sorted order) makes quicksort Θ(n^2). Partition always puts a single element on one side of the partition (Cormen et al.). By randomizing the sort (choosing a random pivot) no particular input elicits 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, left, pivot);
                   quicksort(arr, pivot + 1, right);
              }
         }
    
         private static int randomizedPartition(int[] arr, int left, int right)
         {
              int swapIndex = left + rand.nextInt(right - left) + 1;
              swap(arr, left, swapIndex);
              return partition(arr, left, right);
         }
    
         private static int partition(int[] arr, int left, 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, i, j);
                   else
                        return j;
              }
         }
    
         private static void swap(int[] arr, int i, 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, 0, arr.length - 1);
              System.out.print("After sort: ");
              for (int i = 0; i < 20; i++)
                   System.out.print(arr[i] + " ");
              System.out.println();
         }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My code works great in IE8, Firefox and Safari. But it doesn't work properly
My code works (yeah!) which sends json to a server.. would appreciate any thoughts
This code works fine: $result = $client->__call(optionalInfo, array( new SoapParam(..., client), new SoapParam(..., add_code))
My code works. After I copy about 10 tables I get an error. Dynamic
This code works properly in Firefox but not in Chrome, If you need more
This code works properly in my localhost. I am using xampp 1.7.3. but when
Everything in this code works properly, except the contents of the $1 variable aren't
The below code works properly, but it is hard coded. I would like to
following code works properly but it applies same cell text to all sections. How
This code works: monkey.h @interface monkey : NSObject { NSNumber *monkeyRanch; } @property (nonatomic,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.