Created
November 23, 2024 17:10
-
-
Save vlaleli/3d20487c979a6eef54134fddf3cba959 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
| #include <iostream> | |
| using namespace std; | |
| int main() { | |
| int arr[12] = {99, 85, 72, 64, 58, 50, 45, 33, 25, 12, 7, 3}; | |
| for (int i = 0; i < 12; i++) | |
| { | |
| cout << arr[i] << " "; | |
| } | |
| cout << endl; | |
| int search; | |
| cout << "Enter number to find: "; | |
| cin >> search; | |
| int left = 0, right = 11; | |
| int mid; | |
| bool found = false; | |
| while (left <= right) | |
| { | |
| mid = (left + right) / 2; | |
| if (arr[mid] == search) | |
| { | |
| found = true; | |
| break; | |
| } else if (arr[mid] > search) | |
| { | |
| left = mid + 1; | |
| } else | |
| { | |
| right = mid - 1; | |
| } | |
| } | |
| if (found) | |
| { | |
| cout << "Number found. Index: " << mid << endl; | |
| } else | |
| { | |
| cout << "Not found" << endl; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment