C Display Number Comparisons Used Binary Search Array Consists 1000 Elements Randomly Gene Q37060526

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

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.