Created
October 20, 2014 18:00
-
-
Save shemul/afcefb8c46c4ea0d029d to your computer and use it in GitHub Desktop.
binary search
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
using namespace std; | |
int main() | |
{ | |
int roll[10]; | |
int element ; | |
cout<<"How much roll you want to input ? : " ; | |
cin >> element ; | |
for(int i = 0 ; i < element ; i++) | |
{ | |
cin >> roll[i]; | |
} | |
cout<<"Which roll you want to search ? "<<endl ; | |
int searchs; | |
cin >> searchs ; | |
int first = 0; | |
int last = element-1 ; | |
int middle = (first+last) /2 ; | |
while (first <= last) | |
{ | |
if(roll[middle]<searchs) | |
{ | |
first = middle+1 ; | |
} else if(searchs == roll[middle]) | |
{ | |
cout<<searchs <<" found at location " << middle+1<<endl; | |
break; | |
} else { | |
last = middle-1 ; | |
} | |
middle =(first+last)/2 ; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment