Created
November 20, 2015 11:06
-
-
Save MinhasKamal/c8d0ccd115bd4063d51f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| /** | |
| * Developer: Minhas Kamal (BSSE-0509, IIT, DU) | |
| * Date: 13.Nov.2013 | |
| **/ | |
| #include <iostream> | |
| #include <fstream> | |
| using namespace std; | |
| int main() | |
| { | |
| ifstream list; //input file stream | |
| list.open("BinarySearch_Input.txt"); | |
| //list.open(argv[1]); | |
| if(!list.is_open()){ //when file is not found | |
| cout << "File is not found!\n"; | |
| return 0; | |
| } | |
| int member; //number of integers | |
| list >> member; | |
| int myArray[member]; //stores the input data | |
| for(int i=0; i<member; i++) //reading the data | |
| list >> myArray[i]; | |
| ///BinarySearch | |
| int element; | |
| cout << "Enter the element: "; | |
| cin >> element; | |
| int left=0, //left location of the myArray | |
| right=member-1, //right location of the myArray | |
| location=-1; //the location of the element | |
| while(right-left > 0){ | |
| int loc = (right+left)/2; | |
| if(myArray[loc]==element){ | |
| location=loc; | |
| break; | |
| } | |
| else if(myArray[loc]>element){ | |
| right = loc-1; | |
| } | |
| else if(myArray[loc]<element){ | |
| left = loc+1; | |
| } | |
| } | |
| if(location==-1) { | |
| cout << "\nElement is not found! \n"; | |
| } | |
| else{ | |
| //cout << "\nThe location is: " << location+1 << endl; | |
| cout << "\n\n"; | |
| for(int i=left; i<=right; i++){ | |
| if(myArray[i]==element) cout << "The location is: " << i+1 << endl; | |
| } | |
| } | |
| list.close(); | |
| return 0; | |
| } |
This file contains hidden or 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
| 100 | |
| 11 | |
| 12 | |
| 15 | |
| 18 | |
| 19 | |
| 21 | |
| 23 | |
| 26 | |
| 29 | |
| 29 | |
| 33 | |
| 34 | |
| 34 | |
| 34 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 39 | |
| 40 | |
| 43 | |
| 45 | |
| 45 | |
| 45 | |
| 45 | |
| 45 | |
| 45 | |
| 45 | |
| 46 | |
| 50 | |
| 53 | |
| 54 | |
| 54 | |
| 54 | |
| 56 | |
| 56 | |
| 56 | |
| 56 | |
| 56 | |
| 57 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 60 | |
| 61 | |
| 64 | |
| 64 | |
| 65 | |
| 65 | |
| 65 | |
| 65 | |
| 65 | |
| 67 | |
| 67 | |
| 67 | |
| 67 | |
| 67 | |
| 68 | |
| 68 | |
| 69 | |
| 69 | |
| 70 | |
| 70 | |
| 70 | |
| 71 | |
| 71 | |
| 73 | |
| 74 | |
| 76 | |
| 76 | |
| 78 | |
| 78 | |
| 78 | |
| 79 | |
| 79 | |
| 81 | |
| 82 | |
| 84 | |
| 85 | |
| 85 | |
| 88 | |
| 89 | |
| 89 | |
| 90 | |
| 90 | |
| 93 | |
| 94 | |
| 94 | |
| 96 | |
| 97 | |
| 97 | |
| 98 | |
| 98 | |
| 99 | |
| 99 | |
| 99 | |
| 100 | |
| 100 | |
| 100 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment