binary_search

binary_search - mid =(first last 2 if(key> numbers[mid...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
Sheet1 Page 1 #include <iostream> using namespace std int main() { // ************** declaration statements *********** int numbers[] = {2, 4, 5, 11, 13, 20, 34, 39, 50, 52, 59, 64, 66, 70, 75, 76, 81, 83, 85, 92} int first = 0 int last = 19 int mid = 0 int key = 0 bool found = false cout << "Enter the value you would like to search for: " cin >> key while (first <= last && !found)
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: { mid = (first + last) / 2 if (key > numbers[mid]) { first = mid + 1 } else if (key < numbers[mid]) { last = mid - 1 } else { cout << "The value" << key << " was found in position " << mid << endl found = true } } if (!found) { cout << "The value " << key << " was not found" << endl } return 0 }// end of main...
View Full Document

Ask a homework question - tutors are online