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

The Archive Base Latest Questions

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

I have seen many example of binary search,many method how to optimize it,so yesterday

  • 0

I have seen many example of binary search,many method how to optimize it,so yesterday my lecturer write code (in this code let us assume that first index starts from 1 and last one is N,so that N is length of array,consider it in pseudo code.code is like this:

L:=1;
R:=N;
while( L<R)
{
m:=div(R+L,2);
 if A[m]> x
{
 L:=m+1;

}
else
{
 R:=m;

}
}

Here we assume that array is A,so lecturer said that we are not waste time for comparing if element is at middle part of array every time,also benefit is that if element is not in array,index says about where it would be located,so it is optimal,is he right?i mean i have seen many kind of binary search from John Bentley for example(programming pearls) and so on,and is this code optimal really?it is written in pascal in my case, but language does not depend.

  • 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-12T15:32:47+00:00Added an answer on June 12, 2026 at 3:32 pm

    It really depends on whether you find the element. If you don’t, this will have saved some comparisons. If you could find the element in the first couple of hops, then you’ve saved the work of all the later comparisons and arithmetic. If all the values in the array are distinct, it’s obviously fairly unlikely that you hit the right index early on – but if you have broad swathes of the array containing the same values, that changes the maths.

    This approach also means you can’t narrow the range quite as much as you would otherwise – this:

    R:=m;
    

    would normally be

    R:=m-1;
    

    … although that would reasonably rarely make a significant difference.

    The important point is that this doesn’t change the overall complexity of the algorithm – it’s still going to be O(log N).

    also benefit is that if element is not in array,index says about where it would be located

    That’s true whether you check for equality or not. Every binary search implementation I’ve seen would give that information.

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

Sidebar

Related Questions

I have seen many samples of Ruby code with this line (for example, http://www.sinatrarb.com/
Assume following code, NSString *str=[[NSString alloc] initWithString:@sagar]; [str autorelease]; I have seen many times
I have seen many posting this issue in SO. I have gone through those
I have seen many answers for this type of question but its not related
I have seen many articles about POCO. What is this?
I have seen many of website are displaying RSS Feeds on their website. Example:
I have seen many php scripts in which you can cascade class method to
I have seen many posts and questions about Mocking a private method but still
This is going to be very simple I know. I have seen so many
I have seen simple example Ajax source codes in many online tutorials. What I

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.