binary_search

binary_search - { mid = (first + last) / 2 if (key...

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 &gt; numbers[mid]) { first = mid + 1 } else if (key &lt; numbers[mid]) { last = mid - 1 } else { cout &lt;&lt; &quot;The value&quot; &lt;&lt; key &lt;&lt; &quot; was found in position &quot; &lt;&lt; mid &lt;&lt; endl found = true } } if (!found) { cout &lt;&lt; &quot;The value &quot; &lt;&lt; key &lt;&lt; &quot; was not found&quot; &lt;&lt; endl } return 0 }// end of main...
View Full Document

Ask a homework question - tutors are online