Skip to content

A repository to allow mentees to open a pull request

Notifications You must be signed in to change notification settings

rishavroy97/spider_task0

Repository files navigation

spider_task0

//program to implement binary search

#include using namespace std;

int main() { int count, i, arr[30], num, first, last, middle; cout<<"how many elements would you like to enter?:"; cin>>count;

for (i=0; i<count; i++)
{
	cout<<"Enter number "<<(i+1)<<": "; 
            cin>>arr[i];
}
cout<<"Enter the number that you want to search:"; 
    cin>>num;
first = 0;
last = count-1;
middle = (first+last)/2;
while (first <= last)
{
   if(arr[middle] < num)
   {
	first = middle + 1;

   }
   else if(arr[middle] == num)
   {
	cout<<num<<" found in the array at the location "<<middle+1<<"\n"; 
            break; 
       } 
       else { 
            last = middle - 1; 
       } 
       middle = (first + last)/2; 
    } 
    if(first > last)
{
   cout<<num<<" not found in the array";
}
return 0;

}

About

A repository to allow mentees to open a pull request

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages