Question

C++ C++ C++ Write a program that will provide evidence as to which algorithm will search...

C++ C++ C++ Write a program that will provide evidence as to which algorithm will search an array faster, Linear (sequential) search or Binary Search. Your program should test many different scenarios. Please help with right answer.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi,

If the data is sorted then binary search algorithm is the best, else linear search is the best

let us assume our data is sorted then :

The worst-case and average-case time complexity for binary search is O(log n).

The worst-case and average-case time complexity is O(n).

As we know that  O(log n) is always lesser than O(n) [ O(log n) < O(n) ]
The following example shows the binary search is better than linear search

/* C++ Program - Binary Search */
        
#include<iostream.h>
#include<conio.h>
void main()
{
        clrscr();
        int n, i, arr[50], search, first, last, middle;
        cout<<"Enter total number of elements :";
        cin>>n;
        cout<<"Enter "<<n<<" number :";
        for (i=0; i<n; i++)
        {
                cin>>arr[i];
        }
        cout<<"Enter a number to find :";
        cin>>search;
        first = 0;
        last = n-1;
        middle = (first+last)/2;
        while (first <= last)
        {
                if(arr[middle] < search)
                {
                        first = middle + 1;

                }
                else if(arr[middle] == search)
                {
                        cout<<search<<" found at location "<<middle+1<<"\n";
                        break;
                }
                else
                {
                         last = middle - 1;
                }
                middle = (first + last)/2;
        }
        if(first > last)
        {
                cout<<"Not found! "<<search<<" is not present in the list.";
        }
        getch();
}
Add a comment
Know the answer?
Add Answer to:
C++ C++ C++ Write a program that will provide evidence as to which algorithm will search...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write and implement a recursive version of the sequential search algorithm. Test the algorithm with an...

    Write and implement a recursive version of the sequential search algorithm. Test the algorithm with an array that have ten integer values, user can enter the values or assigned the values to the array in the main method. The search key should be a value in the array or not in the array. JAVA!!!

  • a. You need write a sequential search algorithm that is recursive format. b. Write a binary...

    a. You need write a sequential search algorithm that is recursive format. b. Write a binary search algorithm that is recursive, then write the program to implement it. C++

  • Language = c++ Write a program to find the number of comparisons using the binary search...

    Language = c++ Write a program to find the number of comparisons using the binary search and sequential search algorithms as follows: o Suppose list is an array of 1000 elements. o Use a random number generator to fill the list. o Use the function insertOrd to initially insert all the elements in the list. o You may use the following function to fill the list: void fill(orderedArrayListType& list) {       int seed = 47; int multiplier = 2743;                                ...

  • ***PLEASE USE C++ LANGUAGE*** Binary Search of Strings 1. Write a version of the selection sort...

    ***PLEASE USE C++ LANGUAGE*** Binary Search of Strings 1. Write a version of the selection sort algorithm presented in the unit, which is used to search a list of strings. 2. Write a version of the binary search algorithm presented in the unit, which is used to search a list of strings. (Use the selection sort that you designed above to sort the list of strings.) 3. Create a test program that primes the list with a set of strings,...

  • In C++: Write a C++ function binsearch that carries out the binary search algorithm on a...

    In C++: Write a C++ function binsearch that carries out the binary search algorithm on a sorted array of integers. Your function takes as parameters an array of integers (sorted in increasing order), the size of the array, and a target integer to search for. The function returns the index of the target in the array, and returns -1 if the target is not in the array. Using the code below, add your binsearch function to this C++ program. (Do...

  • Course: CIS-5, Intro to Programming 1. Which algorithm is also called the sequential search? binary search...

    Course: CIS-5, Intro to Programming 1. Which algorithm is also called the sequential search? binary search linear search bubble sort selective sort 2 Which search algorithm requires that the elements be pre-sorted? linear search bubble sort binary search We were unable to transcribe this image

  • Write a C++ function binsearch that carries out the binary search algorithm on a sorted array...

    Write a C++ function binsearch that carries out the binary search algorithm on a sorted array of integers. Your function takes as parameters an array of integers (sorted in increasing order), the size of the array, and a target integer to search for. The function returns the index of the target in the array, and returns -1 if the target is not in the array. The file main1.txt on the webpage contains code that generates a specific array of integers...

  • Benchmark Searching and Sorting Write a program that has an array of at least 20 strings...

    Benchmark Searching and Sorting Write a program that has an array of at least 20 strings that you will have your user enter. It should call a function that uses the linear search algorithm to locate one of the values. The function should keep a count of the number of comparisons it makes until it finds the value. The program then should call a function that uses the binary search algorithm to locate the same value. It should also keep...

  • Write and test a function in C++ that uses the binary search algorithm to search an...

    Write and test a function in C++ that uses the binary search algorithm to search an array of sorted strings – use a do..while loop to allow user to perform multiple searches w/o terminating the program – see sample output below. Use this name array: string names[SIZE] = { "Collins, Bill", "Smith, Bart", "Allen, Jim",         "Griffin, Jim", "Stamey, Marty", "Rose, Geri", "Taylor, Terri",         "Johnson, Jill", "Allison, Jeff", "Looney, Joe", "Wolfe, Bill",         "James, Jean", "Weaver, Jim", "Pore, Bob",...

  • (a) Implement Counting Inversions algorithm in C++. Provide the complete program here which returns the count...

    (a) Implement Counting Inversions algorithm in C++. Provide the complete program here which returns the count and the array. (b) Test your code using this input: a = [2, 6, 1, 5, 4, 3] where array a is the list of your ranking for the given 6 songs.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT