Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C + + Binary search array input I want to be able to input an array of 1 or 2 5 or 1 0 0

C++ Binary search array input I want to be able to input an array of 1 or 25 or 100 is their a better way then how I did it? cout << "Please input a number to find: ";
cin >> n;
auto start = chrono::steady_clock::now();
int binarySearch(int arr[], int l, int r, int n);
// Arrays are shorted 1,25,100
//int arr[]={2};
//int arr[]={0,2,4,6,8,10,12,14,16,18,20,22,24,26,27,28,29,30,31,32,33,34,35,36,38,40,42,};
int arr[]={0,2,4,6,8,10,12,14,16,18,20,22,24,26,27,28,29,30,31,32,33,34,35,36,38,40,42,
50,52,54,56,58,60,62,64,66,68,70,72,74,76,78,80,82,84,86,88,89,90,91,92,93,94,95,96,97,
98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143};
int x = sizeof(arr)/ sizeof(arr[0]);
int result = binarySearch(arr,0, x -1, n);
if (result ==-1){
cout << "Element is not present in array" << endl;
}
else {
cout << "Element is present at index "<< result << endl;
}
auto end = chrono::steady_clock::now();
double elapsed_time_ns = double(chrono::duration_cast (end - start).count());
cout << "Elapsed Time(s)"<< elapsed_time_ns /1e9<<" nanoseconds "<< endl;

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The World Wide Web And Databases International Workshop Webdb 98 Valencia Spain March 27 28 1998 Selected Papers Lncs 1590

Authors: Paolo Atzeni ,Alberto Mendelzon ,Giansalvatore Mecca

1st Edition

3540658904, 978-3540658900

More Books

Students also viewed these Databases questions