Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

QUESTIONS TO BE ANSWERED? WHAT IS THE NUMBER OF COMPARISONS MADE BY SEQUENTIAL SEARCH WHILE SEARCHING FOR 45000? HOW MANY COMPARISONS WERE MADE IN BINARY

QUESTIONS TO BE ANSWERED? WHAT IS THE NUMBER OF COMPARISONS MADE BY SEQUENTIAL SEARCH WHILE SEARCHING FOR 45000?

HOW MANY COMPARISONS WERE MADE IN BINARY SEARCH WHILE SEARCHING FOR 45000?

IN C++"In this exercise you will explore the performance difference between sequential search and binary search. To do so write a program that performs the following tasks:

Prompt the user for a file containing 100,000 unsorted integers

Read those integers into an array

Prompt the user for a search item

Search for that item (using sequential search) and report the number of comparisons required.

Sort the array. Note that this will take about a minute.

Search for that item again (using binary search) and report the number of comparisons required.

You will need to modify both of the search functions to report the number of comparisons that were made during the search.

Use your program and the file of 100,000 integers provided here to answer the six questions in the quiz."

My problem is, the code gave me the correct answer for the first two questions, but not the last two. Please help.

QUESTION 1: Was search item 20000 found? ANSWER IS YES

QUESTION 2: How many comparisons did the sequential search make while searching the list for the value 20000?

Answer: 92102 ANSWER IS CORRECT

Question 3:

How many comparisons did the binary search report in the search for 20000?

Answer: 17 Answer is correct

QUESTION 4: Was the value 45000 found in this list? Answer is false, which is correct

QUESTION 5:

How many comparisons were made by sequential search while searching for 45000?

Answer: 0 ANSWER IS INCORRECT, THIS IS WHAT I GOT WHEN I INPUT 45000 NUMBER INTO THE CODE

How many comparisons were made by binary search while searching for 45000?

Answer: 0 ANSWER IS INCORRECT, THIS IS WHAT I GOT WHEN I INPUT 45000 NUMBER INTO THE CODE

HERE IS THE CODE I USED:

#include #include #include using namespace std; int countBinaryItreations = 0; //for counting binary iteration int linearSearch(int array[], int l, int x) //linear search function { int y = 0; //variable for counting number of iteration for (int i = 0; i < l; i++) //loop used for searching element { if (array[i] == x) { y = i; break; } } if (y != 0) //if y is not 0 then we will have to increase y by 1, due to it starting from 0 y++; return y; } int binarySearch(int arr[], int l, int r, int x)//binary search { countBinaryItreations++; //increase counter by one each time the function hits in till the if (r >= l) { int mid = l + (r - l) / 2; //calculates mid if (arr[mid] == x) //if number is found then return the position return mid; if (arr[mid] > x) //if the mid element is greater than number to be found then search lower than id elements return binarySearch(arr, l, mid - 1, x); return binarySearch(arr, mid + 1, r, x); //binary search on rest half of the array } return -1; //returns -1 if element not found } int main() { int arr[100000]; //defining the array fstream f; //defining an fstream variable string str = "Text.txt"; //INPUT FILE name, you can change it to whichever file you want to take input from f.open(str, ios::in); //opening the file in input mode if (!f) //if file not opened output Not Opened cout << "Not opened"; int x, len = 0; //x is variable for taking input from file, len to count the number of inputs while (f >> x) //taking input from file { arr[len] = x; len++; } int number; //variable to store the number which we have to search cout << "Enter a number you want to search in the file:"; cin >> number; x = linearSearch(arr, len, number); //calling linear search cout << " Number of iteration required in LINEAR SEARCH is " << x;//x is 0 if not found, else the number of itreation it took sort(arr, arr + len - 1); //sorting the array x = binarySearch(arr, 0, len - 1, number); //calling bnary search function if (x != -1) //x!= -1 means the element found so printing the number of itreation it took cout << " Number of iteration required in BINARY SEARCH is " << countBinaryItreations;// else //if x==-1 then element not found cout << " Number of iteration required in BINARY SEARCH is 0"; }

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

Databases Demystified

Authors: Andrew Oppel

1st Edition

0072253649, 9780072253641

More Books

Students also viewed these Databases questions

Question

What is Change Control and how does it operate?

Answered: 1 week ago

Question

How do Data Requirements relate to Functional Requirements?

Answered: 1 week ago