sbshah97 / DS-Algo-Handbook

[ONGOING] ACM ICPC Handbook for Algorithms and Data Structures

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Array not sorted in binary_search.cpp

aedorado opened this issue · comments

Ground rules:

  1. Whoever claims the Issue first, should send a Pull Request within 24 hours else it'll be assigned to another claimant and further Pull Requests regarding that issue will not be entertained.
  2. Pull Requests sent without claiming and referencing the Issue will not be entertained.
  3. Make sure your Pull Requests have just one commit in them. Squash multiple commits into one if you have to.

Check out this link to understand better.

The array is not sorted in binary-search.cpp

Do you want binary-search.cpp to be corrected?

`#include
using namespace.std;
void main()
{
clrscr();
int n, i, arr[50], search, first, last, middle;
cout<<"Enter the elements in sorted order:";
cin>>n;
cout<<"Enter "<<n<<" number :";
for (i=0; i<n; i++)
{
cin>>arr[i];
}
cout<<"Enter a number to find :";
cin>>search;
first = 0;
last = n-1;
middle = (first+last)/2;
while (first <= last)
{
if(arr[middle] < search)
{
first = middle + 1;

    }
    else if(arr[middle] == search)
    {
        cout<<search<<" found at location "<<middle+1<<"\n";
        break;
    }
    else
    {
         last = middle - 1;
    }
    middle = (first + last)/2;
}
if(first > last)
{
    cout<<"Not found! "<<search<<" is not present in the list.";
}

}`

Hey @AnanyaSharma22 kindly send a PR instead of commenting on the code here.
@TuhinKundu yes, kindly send a PR referencing this issue.
@aedorado Thanks for pointing it out! 👍

See PR has been merged regarding this. Is this issue still open?

The Pull Request has been sent, @TuhinKundu