(Solved) : Modify C Program Shows Three Functions Common Search Algorithms Linear Sorted Binary Demon Q37238515 . . .
Modify this C++ program which shows the three functions ofcommon search algorithms: linear, sorted, and binary. Todemonstrate and compare the relative efficiency of each one, ladd ametric that counts the number of comparisons that each oneperforms.
The code below contains a working program that creates an arrayof test data, another array of search keys, and then runs thesearch algorithms. The linear search already contains a statementwhich counts its comparisons. Your job now is to modify thesortedSearch and binarySearch functions so that they also trackthese metrics. Use the global variables that are alreadydeclared.
Screenshot output samples
CODE:
#include <iostream>
#include <ctime>
using namespace std;
// Global variables (JUST THIS ONCE)
long linearCount = 0;
long sortedCount = 0;
long binaryCount = 0;
int linearSearch(int numbers[], int size, int key)
{
for (int i = 0; i < size; i++)
{
linearCount++;
if (numbers[i] == key)
return i;
}
return -1;
}
void bubblesort(int numbers[], int size)
{
for (int pass = 0; pass < size; pass++)
{
for (int i = 0; i < size-1;i++)
{
if (numbers[i]> numbers[i + 1])
{
int temp = numbers[i];
numbers[i] = numbers[i + 1];
numbers[i + 1] = temp;
}
}
}
}
void display(int numbers[], int size)
{
for (int i = 0; i < size; i++)
cout << numbers[i] << “”;
cout << endl;
}
int sortedSearch(int numbers[], int size, int key)
{
int i = 0;
while (i < size && numbers[i] <=key)
{
if (numbers[i] == key)
return i;
i++;
}
return -1;
}
int binarySearch(int numbers[], int size, int key)
{
int begin = 0;
int end = size – 1;
while (begin <= end)
{
int middle = (begin + end) /2;
if (numbers[middle] == key)
returnmiddle;
else if (key <numbers[middle])
end = middle -1;
else
begin = middle +1;
}
return -1;
}
int main()
{
const int NUM_SEARCHES = 20;
const int SIZE = 1000;
int numbers[SIZE];
int searchKeys[NUM_SEARCHES];
srand(time(0));
for (int i = 0; i < SIZE; i++)
numbers[i] = rand() % (SIZE *5);
for (int i = 0; i < NUM_SEARCHES; i++)
searchKeys[i] = rand() % (SIZE *5);
// Linear search test
for (int i = 0; i < NUM_SEARCHES; i++)
{
int index = linearSearch(numbers,SIZE, searchKeys[i]);
if (index == -1)
cout <<“Search key ” << searchKeys[i] << ” not found.”<< endl;
else
cout <<“Search key ” << searchKeys[i] << ” found at index “<< index << “.” << endl;
}
bubblesort(numbers, SIZE);
display(numbers, SIZE);
// Sorted search test
for (int i = 0; i < NUM_SEARCHES; i++)
{
int index = sortedSearch(numbers,SIZE, searchKeys[i]);
if (index == -1)
cout <<“Search key ” << searchKeys[i] << ” not found.”<< endl;
else
cout <<“Search key ” << searchKeys[i] << ” found at index “<< index << “.” << endl;
}
// Binary search test
for (int i = 0; i < NUM_SEARCHES; i++)
{
int index = binarySearch(numbers,SIZE, searchKeys[i]);
if (index == -1)
cout <<“Search key ” << searchKeys[i] << ” not found.”<< endl;
else
cout <<“Search key ” << searchKeys[i] << ” found at index “<< index << “.” << endl;
}
cout << endl << “SUMMARY:” <<endl;
cout << “Linear search comparisons: ” <<linearCount << endl;
cout << “Sorted search comparisons: ” <<sortedCount << endl;
cout << “Binary search comparisons: ” <<binaryCount << endl;
// keep this stuff at the end of main
cout << endl;
system(“pause”);
return 0;
}
Expert Answer
Answer to Modify this C++ program which shows the three functions of common search algorithms: linear, sorted, and binary. To demo…
OR