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 8884993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:12:00+00:00 2026-06-14T21:12:00+00:00

I am creating quicksort to sort other city nearest to a specific city. Here

  • 0

I am creating quicksort to sort other city nearest to a specific city.

Here is my code:

    private static void QuickSort(ArrayList<City> array, int First, int Last, City city ){
    int Low,High,MidDistance;
    Low = First;
    High = Last;
    Edge compareEdge = new Edge(array.get((First + Last)/ 2), city);
    MidDistance = compareEdge.computeDistance();

    do {
        compareEdge = new Edge(array.get(Low),city);
        while (compareEdge.computeDistance() < MidDistance){
            Low += 1;
            compareEdge = new Edge(array.get(Low),city);
        }

        compareEdge = new Edge(array.get(High),city);
        while (compareEdge.computeDistance() > MidDistance){
            High -= 1;
            compareEdge = new Edge(array.get(High),city);
        }

        if (Low <= High) {
                Swap(array,Low,High);
                Low = Low + 1;
                High = High - 1;
        }

    } while (Low <= High);

    if (First < High){
        QuickSort(array, First, High, city);
    }

    if (Low < Last){
        QuickSort(array, Low, Last, city);
    }

}

private static void Swap(ArrayList<City>array,int Low, int High){
    City tempCtiy = array.get(Low);
    array.get(Low).set(array.get(High));
    array.get(High).set(tempCtiy);
}

This code is inside my MST class. so when I run it

Exception in thread “main” java.lang.IndexOutOfBoundsException: Index: 49, Size: 49

it happens here :

    while (compareEdge.computeDistance() < MidDistance){
        Low += 1;
        compareEdge = new Edge(array.get(Low),city);
    }

Help me to figure out what’s wrong.
It works well when I write in VB.net.

  • 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-06-14T21:12:02+00:00Added an answer on June 14, 2026 at 9:12 pm

    Add the termination condition in while on Low as below:

          while (compareEdge.computeDistance() < MidDistance &&
                  && Low < array.size()-1){
                  Low += 1;
                  compareEdge = new Edge(array.get(Low),city);
          }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Creating a view with following code. - (void)loadView { paintView=[[UIView alloc]initWithFrame:CGRectMake(0, 50, 320, 430)];
Creating a batch script in Windows XP. Here's a snippet of code I'm having
Creating the closure is easy but using it is confusing for me. Here is
creating 20 viewcontrollers and calling them one after the other from the mainviewcontroller as
can someone look at this code and tell me if I am creating the
Creating Traversals for Binary Search Tree with Recursion. void inOrder(void (*inOrderPtr)(T&)) { if(this->left !=
Creating servlet that implements contextInitializer interface in this code, then accessing file inside contextinitialized()
Creating a array on the stack with the size determined at runtime is allowed
Creating custom IHttpModules, I have realized that the requests for static files (e.g.: .css
Creating some regex expressions here. I was wondering if I could get some pointers

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.