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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:35:09+00:00 2026-06-04T20:35:09+00:00

suppose I have an array of 10 ints, and I’m using binary search to

  • 0

suppose I have an array of 10 ints, and I’m using binary search to find a number, let’s take for example numbers

1 2 3 4 5 6 7 8 9 10

and I’m using this method

static void binarySearch(int n, int[] a, int low, int high)
    {
        int mid = (high + low) / 2;
        if(low > high)
            System.out.println(n+" was not found after "+counter+" comparisons");
        else if(a[mid] == n)
        {
            counter++;
            System.out.println(n+" was found at position "+mid+" after "+counter+" comparisons");
        }            
        else if(a[mid] < n)
        {
            counter++;
            binarySearch(n, a, mid+1, high);
        }            
        else
        {
            counter++;
            binarySearch(n, a, low, mid-1);
        }            
    }

what is the proper way of calling the method binarySearch(5, a, 0, a.lenght)
or
binarySearch(5, a, 0, a.lenght-1)

I know they both will find the number, but they will find it at different index; thus taking more comparison

  • 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-04T20:35:11+00:00Added an answer on June 4, 2026 at 8:35 pm

    Well let’s do some tests shall we?

    First, let’s search for each number in the array. We get:

    binarySearch(i, array, 0, array.length);

    1 was found at position 0 after 3 comparisons
    2 was found at position 1 after 4 comparisons
    3 was found at position 2 after 2 comparisons
    4 was found at position 3 after 3 comparisons
    5 was found at position 4 after 4 comparisons
    6 was found at position 5 after 1 comparisons
    7 was found at position 6 after 3 comparisons
    8 was found at position 7 after 4 comparisons
    9 was found at position 8 after 2 comparisons
    10 was found at position 9 after 3 comparisons
    Average: 2.9 comparisons
    

    binarySearch(i, array, 0, array.length - 1);

    1 was found at position 0 after 3 comparisons
    2 was found at position 1 after 2 comparisons
    3 was found at position 2 after 3 comparisons
    4 was found at position 3 after 4 comparisons
    5 was found at position 4 after 1 comparisons
    6 was found at position 5 after 3 comparisons
    7 was found at position 6 after 4 comparisons
    8 was found at position 7 after 2 comparisons
    9 was found at position 8 after 3 comparisons
    10 was found at position 9 after 4 comparisons
    Average: 2.9 comparisons
    

    As you can see, variances do appear, but the average remains constant.
    Now let’s test for bigger numbers:

    100000 items
    binarySearch(i, array, 0, array.length);
    Average: 15.68946 comparisons
    binarySearch(i, array, 0, array.length - 1);
    Average: 15.68946 comparisons
    
    200000 items
    binarySearch(i, array, 0, array.length);
    Average: 16.689375 comparisons
    binarySearch(i, array, 0, array.length - 1);
    Average: 16.689375 comparisons
    
    500000 items
    binarySearch(i, array, 0, array.length);
    Average: 17.951464 comparisons
    binarySearch(i, array, 0, array.length - 1);
    Average: 17.951464 comparisons
    

    Hence, on average it doesn’t go either way. For the sake of convention I would advise using the exclusive upper bound version: binarySearch(i, array, 0, array.length);

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

Sidebar

Related Questions

Using ActionScript 3, suppose I have an array of numbers, lets say: 1, 2,
Suppose I have an array like this: $array = array(a,b,c,d,a,a); and I want to
Suppose you have an array of 1000 random integer numbers and you need to
I have a problem with PHP usort() . Let's suppose I have an array
Suppose i have a array like this : Array( '1' => Array( ID =>
Suppose i have array of characters. say char x[100] Now, i take input from
Suppose I have an integer array like this: {5,3,5,4,2} and I have a method
Suppose I have this array of hashes: [ {href=>https://company.campfirenow.com, name=>Company, id=>123456789, product=>campfire}, {href=>https://basecamp.com/123456789/api/v1, name=>Company,
Suppose I have an array like this: $products = array('Shoes' => array('price' => 49.99,
suppose you have an array with a number of strings in ActionScript3 and you

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.