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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:37:35+00:00 2026-06-12T00:37:35+00:00

code like this,in page 44. int binsearch(int x, int v[], int n) { int

  • 0

code like this,in page 44.

int binsearch(int x, int v[], int n) { int low, high, mid; low = 0; high = n - 1; while(low <= high){ mid = (high+low)/2; if(x < v[mid]){ high = mid + 1; }else if(x > v[mid]){ low = mid + 1; }else{ return mid; } printf("mid is %d\n",mid); } return -1; } int main(void) { int v[] = {2,3,4,7,8,23,54,65,76}; int ret = binsearch(7, v, sizeof(v)/sizeof(int)); printf("%d,ret is %d\n", sizeof(v),ret); return 0; }

compile and run it,dead loop as a result! so the line “low = mid + 1;” should take as “low = mid;”, is that right?thx.

  • 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-12T00:37:36+00:00Added an answer on June 12, 2026 at 12:37 am

    The problem in the code you posted is not the value of low, but in the test for the setting of the value of high … if x is less than v[mid], then the new index value of high should be one less than, not greater than mid. So you want to change the following:

    if(x < v[mid]){
        high = mid + 1;
    

    to:

    if(x < v[mid]){
        high = mid - 1;
    

    You can see a working example here: http://ideone.com/E6Ma8

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

Sidebar

Related Questions

I have an html page with a link like this: <a href=javascript:window.print()>Print QR code</a>
I am using page.registerstartupscript in my code behind C#. Its like this: string item1=category1;
My SQL table looks like this: CREATE TABLE Page ( Id int primary key,
I have a page, Default.aspx, with its own code-behind file like this: using System;
I want to file Upload The code is like this : int iTotal =
Code like this works well. Intent configIntent = new Intent (context, WidgetConfigActivity.class); configIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
The code like this: $(.hide).click(function(){ $(.online-contact).animate({width: 'toggle',height: '100%'}, fast); $(this).hide(); $(.show-class).show(); }) I want
Suppose code like this: class Base: def start(self): pass def stop(self) pass class A(Base):
In code like this, get '/posts.?:format?' do # ... end How can I get
I have some jQuery code like this : $(function($) { $('input.autonumeric').autoNumeric({aSep: ',', aDec: '.',vMax:'1000000000000'});

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.