C++!
Display the number of comparisons used in binary search.
You have an array that consists of 1000 elements that arerandomly generated and another array that have five randomlygenerated numbers. These five numbers should be searched in thefirst array, and the number of comparisons should be displayed.
Solution
C++ code:-
//code starts here
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
//bubble sort for sorting the array
int bublesort( int arr[],int SIZE)
{
int a = 0;
bool flag = true;
do
{
flag = false;
for(int i = 0; i<SIZE-1 ; i++)
{
if(arr[i] > arr[1+i])
{
a= arr[1+i];
arr[i+1] = arr[i];
arr[i]= a;
flag = true;
}
}
}
while(flag);
return 0;
}
//do binary search and return no. of comparison and return -1 ifnot f
int binarysearch(int
OR
OR