Question

In: Computer Science

Programing language C++ In this exercise you will explore the performance difference between sequential search and...

Programing language 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 might take a few minutes.

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.

Solutions

Expert Solution

I've added code to generate file to store random numbers

Code with comments

#include <bits/stdc++.h>

using namespace std;

int linear_search(int arr[], int n, int val) {  // sequentail search

    for (int i = 0; i < n; i++) {

        if (arr[i] == val) {

            return i + 1;

        }

    }

    return n;

}

int binary_search(int arr[], int n, int val) {  // binary search

    int start = 0, end = n - 1, cnt = 0;

    while (start <= end) {

        int mid = (start + end) / 2;

        cnt++;

        if (arr[mid] == val) return cnt;

        if (arr[mid] > val)

            end = mid - 1;

        else

            start = mid + 1;

    }

    return cnt;

}

int main() {

    /**** generate data ***/

    // fstream file;

    // file.open("numbers.txt", ios::out);

    // for (int i = 0; i < 100000; i++) {

    //     file << rand() % 10000 << " ";

    // }

    // file.close();

    string filename;

    cout << "Enter file name containing numbers: ";

    cin >> filename;

    fstream file;

    file.open(filename);

    int arr[100000], n = 0, num;

    while (file >> num) arr[n++] = num;  // read into array

    cout << "Enter number to search: ";

    cin >> num;

    cout << "Comparisons required in sequential search: "

         << linear_search(arr, n, num) << endl;

    sort(arr, arr + n);  // sort array

    cout << "Enter number to search: ";

    cin >> num;

    cout << "Comparisons required in sequential search: "

         << binary_search(arr, n, num) << endl;

}


Related Solutions

in C++ programing language Write a program that prompts the user for an integer, then prints...
in C++ programing language Write a program that prompts the user for an integer, then prints all of the numbers from one to that integer, separated by spaces. Use a loop to print the numbers. But for multiples of three, print "Fizz" instead of the number, and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz". Drop to a new line after printing each 20 numbers. If the user typed...
c programing language When a new program is executed, a new process is created with the...
c programing language When a new program is executed, a new process is created with the next available process ID. However, there are a few special processes that always have the same process ID, which are usually given the ID value less than 5 these are called system processes. Can you identify which of the two system processes have the process ID of 0 and 1 respectively?
I need programing in C language (not C#,C++) Sheldon Game RPSLS: - Stone: Win against Scissors...
I need programing in C language (not C#,C++) Sheldon Game RPSLS: - Stone: Win against Scissors who destroys and against Lizard who bursts,ties with himself, and loses to Rock Covering Paper and Spock that vaporizes the stone. - Role: Win against Stone who he covers and against Spock who refutes, tie with himself, and loses against Scissors who cut it and against Lizard who eat. - Scissors: Win against Paper who cuts and against Lizard who decapitates, tie with himself,...
For C++ Function 1: Write a recursive function to perform a sequential search on a set...
For C++ Function 1: Write a recursive function to perform a sequential search on a set of integers The function will require an array parameter and the number to look for. These are the minimal parameter requirements The function should take an array of any size Function 2: Write a recursive function that will convert an integer (base 10) to binary The function should only have an integer parameter Have the function write the binary number to the console You...
. In this exercise we will evaluate the performance difference between two CPU architectures: CISC (Complex...
. In this exercise we will evaluate the performance difference between two CPU architectures: CISC (Complex Instruction Set Computing) and RISC (Reduced Instruction Set Computing). Generally speaking, CISC CPUs have more complex instructions than RISC CPUs and therefore need fewer instructions to perform the same tasks. However, typically one CISC instruction, since it is more complex, takes more time to complete than a RISC instruction. Assume that a certain task needs P CISC instructions and 2P RISC instructions, and that...
difference between performance assesment and performance appraisal
difference between performance assesment and performance appraisal
In C language: What is the difference between the step and stepi gdb commands? Why is...
In C language: What is the difference between the step and stepi gdb commands? Why is this difference significant?
C programing language A file "data.txt" contains only integers. Write a code to find average of...
C programing language A file "data.txt" contains only integers. Write a code to find average of all values and print the average How would you use execlp function to execute "ps –e –a –l" command char *dt = "The five boxing wizards jump quickly"; write a program to count frequency of each letter, ignore case. Print the letter and frequency of each letter. // 1A: . Ask the user to enter a password string, store it in pass. Password should...
c programing language This assignment, which introduces the use of loops, solves the following problem: given...
c programing language This assignment, which introduces the use of loops, solves the following problem: given a starting location in a city, how long does it take a “drunken sailor” who randomly chooses his direction at each intersection to reach the city’s border? You will read input values to set up the problem parameters, run several trials to determine an average number of steps for the sailor to reach the border, and output the results. This problem is an example...
This question will ask you about the sequential search theory of unemployment. a) What is the...
This question will ask you about the sequential search theory of unemployment. a) What is the reservation wage? b) What does sequential search theory predict about its behavior over the unemployment spell? c) In the data, reservation wages typically decline over the unemployment spell. Provide 3 possible reasons why this might be the case. d) Are declining reservation wages consistent or inconsistent with negative duration dependence? e) How might you reconcile the empirical regularities of (i) declining reservation wages and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT