Skip to content

Instantly share code, notes, and snippets.

@vlaleli
Created November 23, 2024 17:10
Show Gist options
  • Select an option

  • Save vlaleli/3d20487c979a6eef54134fddf3cba959 to your computer and use it in GitHub Desktop.

Select an option

Save vlaleli/3d20487c979a6eef54134fddf3cba959 to your computer and use it in GitHub Desktop.
#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