Question

In: Computer Science

IN C++ Write a program to find the number of comparisons using binarySearch and the sequential...

IN C++

Write a program to find the number of comparisons using binarySearch and the sequential search algorithm as follows:
Suppose list is an array of 1000 elements.

2 Use any sorting algorithm to sort list.

Solutions

Expert Solution

Time complexity: sequential search = O(n), Binary Search = O(logn)

code:  

#include <iostream>
#include <ctime>
#include <unistd.h>
using namespace std;

static int comparison = 0;

int binarySearch(int arr[], int num, int lo, int hi)
{
    if(lo <= hi)
    {
        int mid = lo + (hi - lo)/2;
        comparison = comparison + 1;
        if(num == arr[mid])
        {

            return mid;
        }

        if(num < arr[mid])
        {
            return binarySearch(arr,num,lo,mid-1);
        }
        if(num > arr[mid])
        {
            return binarySearch(arr,num,mid+1,hi);
        }
    }
    return -1;
}

int main()
{
    int arr[1000];
    srand(time(0));
    for(int i = 0; i < 1000; i++)
    {
        arr[i] = int(rand()/10);
    }
    //cout << arr[100]; 
    int temp;

    int num;
    int found;
    //int comparison;
    cout << "Enter the number to search: " <<endl;
    cin >> num;
    cout << "Performing sequential search..." <<endl;
    bool fo = false;
    int i = 0;
    for(i = 0; i < 1000 && !fo; i++)
    {
        if(num == arr[i])
        {
            fo = true;
        }
    }
    if(i == 1000)
    {
        cout << " Your number " << num << "not present!" <<endl;
    }else
    {
        cout << " Your number " << num << " found at index: " << i-1 << " using sequential search by comparing " << i << " elements" << endl;
    }

    for(int i1 = 0; i1 < 1000; i1++) // sorting the array to apply binary search, using bubble sort
    {
        for(int j1 = 1; j1 < 1000; j1++)
        {
            if(arr[j1-1] > arr[j1])
            {
                temp = arr[j1-1];
                arr[j1-1] = arr[j1];
                arr[j1] = temp;
            }
        }
    }
    cout << "\n \n Performing binary search..." << endl;
    sleep(1);
    found = binarySearch(arr, num, 0 , 999);

    if(found == -1)
    {
        cout << "Your number not present!";
    }else
    {
        cout << "your number " <<num <<  " found after comparing  " << comparison << " numbers using binary search.";
    }

    return 0;
}

Related Solutions

IN C++ Write a program to find the number of comparisons using binarySearch and the sequential...
IN C++ Write a program to find the number of comparisons using binarySearch and the sequential search algorithm as follows: Suppose list is an array of 1000 elements. 3 Search list for some items as follows: a. Use the binary search algorithm to search the list. (You may need to modify the algorithm given in this chapter to count the number of comparisons.) b. Use the binary search algorithm to search the list, switching to a sequentialsearch when the size...
In C++ Write a program to find the number of comparisons using binarySearch and the sequential...
In C++ Write a program to find the number of comparisons using binarySearch and the sequential search algorithm as follows: Suppose list is an array of 1000 elements. 5.1 Use a random number generator to fill list.
Question 5 Write a program to find the number of comparisons using binarySearch and the sequential...
Question 5 Write a program to find the number of comparisons using binarySearch and the sequential search algorithm as follows: Suppose list is an array of 1000 elements. 5.1 Use a random number generator to fill list. 5.2 Use any sorting algorithm to sort list. 5.3 Search list for some items as follows: a. Use the binary search algorithm to search the list. (You may need to modify the algorithm given in this chapter to count the number of comparisons.)...
In JAVA!!! write a method called Comparisons that will return the number of comparisons to find...
In JAVA!!! write a method called Comparisons that will return the number of comparisons to find an element in the tree. The main program calls this method for each element, adding the comparisons each time in order to count the total number of comparisons. The program then outputs the total number of comparisons and the average number. You may use the program BuildTreeWIthMethod to build your tree. Then, after you have made the call to inOrder(root), add the following code:...
write a method called Comparisons that will return the number of comparisons to find an element...
write a method called Comparisons that will return the number of comparisons to find an element in the tree. The main program calls this method for each element, adding the comparisons each time in order to count the total number of comparisons. The program then outputs the total number of comparisons and the average number. You may use the program BuildTreeWIthMethod to build your tree. Then, after you have made the call to inOrder(root), add the following code: int totalComparisons=0;...
write C++ program using functions (separate function for each bottom) Write a program to find if...
write C++ program using functions (separate function for each bottom) Write a program to find if a number is large word for two given bottom base - bottom1 and bottom2. You can predict that a number, when converted to any given base shall not exceed 10 digits. . the program should ask from user to enter a number that it should ask to enter the base ranging from 2 to 16 after that it should check if the number is...
Write a C++ program to find the number of pairs of integers in a given array...
Write a C++ program to find the number of pairs of integers in a given array of integers whose sum is equal to a specified number.
• Write a C++ program to find the largest umber, smallest number and sum of all...
• Write a C++ program to find the largest umber, smallest number and sum of all the element of a given array of 20 integers • Note − Declare array to 20 numbers − Input values for 20 array elements − Find largest number − Find smallest number − Find sum of all numbers • Display the results
Write a C program to find out the number of words in an input text file...
Write a C program to find out the number of words in an input text file (in.txt). Also, make a copy of the input file. Solve in C programming.
Using C++ Write a program to compute the number of collisions required in a long random...
Using C++ Write a program to compute the number of collisions required in a long random sequence of insertions using linear probing, quadratic probing, and double hashing.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT