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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:34:45+00:00 2026-06-12T13:34:45+00:00

I am making a java program that uses the interpolation search below I have

  • 0

I am making a java program that uses the interpolation search below I have gotten from wikipedia. In my main program I have created an int array that will have 100,000 spots. Then I fill all those spots with random numbers and sort it. I then generate a random search key and call the function. I am also looping the function call 100 times each time with a different search key. When I do this I get an array out of bounds error on this statement if (sortedArray[mid] < toFind). The program works fine with an array with 10 spots, 100 spots, 1000 spots, but when I get to 100,000 I get the error. Do you know what I can do to fix this problem?

 public int interpolationSearch(int[] sortedArray, int toFind){
  // Returns index of toFind in sortedArray, or -1 if not found
  int low = 0;
  int high = sortedArray.length - 1;
  int mid;

  while (sortedArray[low] <= toFind && sortedArray[high] >= toFind) {
   mid = low +
         ((toFind - sortedArray[low]) * (high - low)) /
         (sortedArray[high] - sortedArray[low]);  

   if (sortedArray[mid] < toFind)
    low = mid + 1;
   else if (sortedArray[mid] > toFind)
    // Repetition of the comparison code is forced by syntax limitations.
    high = mid - 1;
   else
    return mid;
  }

  if (sortedArray[low] == toFind)
   return low;
  else
   return -1; // Not found
 }
  • 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-12T13:34:47+00:00Added an answer on June 12, 2026 at 1:34 pm

    You are overflowing the max value that can be stored in an int which is 2,147,483,647.

    When calculating the numerator in your equation you are doing ((39258-0)*(99999-0)) which is 3,925,760,742.

    If you change it to this you won’t have this issue:

    long numerator = (long)(toFind - sortedArray[low]) * (long)(high - low);
    mid = low + numerator / (sortedArray[high] - sortedArray[low]);
    

    I think you also need to change your while loop to be:

    while (sortedArray[low] < toFind && sortedArray[high] >= toFind) {
    

    Otherwise you have a situation where sortedArray[low] == sortedArray[high] == toFind

    Then your equation reduces to

    mid = low + (0 * (high - low)) / 0 = 0/0
    

    Java allows division by zero. I’m not exactly sure what happens though, it may be that in this case mid = infinity or NaN.

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

Sidebar

Related Questions

I'm making a program in java that monitors and backup a directory. From time
I have an java program that runs external programs executing commandline parameters. After making
I am making a program that will have the ability to run the java
I'm making a Java program that accesses a website, which depends on handing out
I'm making a Java program that can solve the roots using the quadratic equation
Im am making an Java SWT program that is required to run on both
Ok, I'm kinda new to java. I'm making a program that solves for one
I have a Java program making some JNI calls to a native library (mylib.so).
I am making a program that retrieves the inputted data/values of fields from the
I have been tasked with making an existing Java program available via a web

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.