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

  • SEARCH
  • Home
  • 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 6901845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:43:28+00:00 2026-05-27T07:43:28+00:00

I am trying to get a quicksort method to sort some or partial data.

  • 0

I am trying to get a quicksort method to sort some or partial data. I think my partition method is correct and my recursive call to quicksort the left segments and right segments is giving me an array out of bounds Exception but i can’t seem to figure it out. here is the code I wrote and am getting the error in

 private void quickSorter(String[] data, int firstIndex, int numberToSort) {
    if (numberToSort > 1) {
        int pivot = partition(data, firstIndex, numberToSort);
        int right = firstIndex + numberToSort -1;
        if (firstIndex < pivot -1)
        quickSorter(data, firstIndex, pivot -1);
        if (pivot  < right && right <data.length)
        quickSorter(data, pivot  , right);
    }
}
private void swap(String[] data ,int tooBigNdx, int tooSmallNdx) {
    String temp = data[tooBigNdx];
    data[tooBigNdx] = data[tooSmallNdx];
    data[tooSmallNdx] = temp;

}

@Override
public int partition(String[] data, int firstIndex, int numberToPartition) {
    int ndx = firstIndex;
    String pivot = data[ndx];
    int tooBigNdx = ndx;
    int tooSmallNdx = firstIndex + numberToPartition -1;
    while (tooBigNdx < tooSmallNdx) {
    while (tooBigNdx < tooSmallNdx && (data[tooBigNdx].compareTo(pivot) < 0 || data[tooBigNdx].equals(pivot)))
        tooBigNdx++;

    while (tooSmallNdx > ndx && (data[tooSmallNdx].compareTo(pivot) > 0 || data[tooSmallNdx].equals(pivot))) 
        tooSmallNdx--;
    if (tooBigNdx < tooSmallNdx ) {
        swap(data, tooBigNdx,tooSmallNdx);
    tooBigNdx++;
    tooSmallNdx--;
    }
    }
    if (pivot.compareTo(data[tooSmallNdx]) > 0 || pivot.equals(data[tooSmallNdx]) ) {
    swap(data, ndx,tooSmallNdx);
    return tooSmallNdx;
    } else return ndx;


}

EDIT :

The data set for testing is randomly created every time and are 5 letter words. I am always getting an arrayoutofBounds exception but the amount it is out of bounds changes depending on the data set.

java.lang.ArrayIndexOutOfBoundsException: -1
    at sortcomparison.BasicSorter.partition(BasicSorter.java:62)
    at sortcomparison.BasicSorter.quickSorter(BasicSorter.java:37)
    at sortcomparison.BasicSorter.quickSorter(BasicSorter.java:40)
    at sortcomparison.BasicSorter.quickSorter(BasicSorter.java:40)
    at sortcomparison.BasicSorter.quickSorter(BasicSorter.java:40)
    at sortcomparison.BasicSorter.quickSort(BasicSorter.java:31)
    at sbccunittest.BasicSorterTester.standardSortTest(BasicSorterTester.java:166)
    at sbccunittest.BasicSorterTester.testQuickSort(BasicSorterTester.java:56)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:69)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:48)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:292)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
  • 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-27T07:43:29+00:00Added an answer on May 27, 2026 at 7:43 am

    Shouldn’t

     int right = firstIndex + numberToSort -1;
    

    be

     int right = firstIndex + numberToSort - 1 - pivot;
    

    Remember, the 2nd parameter is not an index number, it is a count.

    You’re other option is to change it to an index number (which I think would be more intuitive.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying get date of call but it's returning value of 1.1.1970(I think
Trying to get comfortable with jQuery and I have encountered some sample code that
Trying to get my mind around google protobuf. I found some implementation of protobuf
I'm trying get all of the current stack frames and do some inspection on
When I'm trying get method from remote webservice it gives me error. My code
I'm having a bit of trouble trying to get this quicksort algorithm to work
So I'm trying to implement quicksort in ruby and I get this error `quicksort':
I am trying get the data from my database but i am not getting
Trying to get tag-it to work with an ajax call. Everything works so far.
I'm trying get bytes from bitmap in blackberry using the next method in Bitmap:

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.