Question

In: Computer Science

Write a program in C++ to implement Binary Search Algorithm. Assume the data given in an...

Write a program in C++ to implement Binary Search Algorithm. Assume the data given in an array. Use number of data N at least more than 10. The function Binary Search should return the index of the search key V.

Solutions

Expert Solution

ANSWER: Here I am giving you the code and output please like it.

CODE:


#include <iostream>
using namespace std;

int binarySearch(int arr[], int l, int r, int ele)
{
   while (l <= r) {
       int m = l + (r - l) / 2;

       // Checking if ele is present at middle
       if (arr[m] == ele)
           return m;

       // If ele greater, ignoring left half
       if (arr[m] < ele)
           l = m + 1;

       // If ele is smaller then ignoring right half
       else
           r = m - 1;
   }


   return -1;
}

int main(void)
{
       int N;
cout<<"Enter the size of the array: ";
   cin>>N;
   int arr[N];
   cout<<"Enter "<<N<<" elements in the array: \n";
   for(int i=0;i<N;i++){
       cin>>arr[i];
   }
     
   int x;
   cout<<"\nEnter your number to search in the array:";
   cin>>x;
     
  
   int result = binarySearch(arr, 0, N - 1, x);
   (result == -1) ? cout << "Element is not present in array"
               : cout << "Element is present at index " << result;
   return 0;
}

OUTPUT:


Related Solutions

Assume you need to write a Java program that uses a binary search algorithm to search...
Assume you need to write a Java program that uses a binary search algorithm to search a sorted array for a given value. 1. Write a Java pseudocode that uses recursion to accomplish the task. Here is a hint. When you are searching for a particular value in an array, there are two possible outcomes. 1) The value is found and the array index of that value is returned. 2) The value is not found and we return -1. (5...
Make a Binary search program for C# and write algorithm and explain it in easy words...
Make a Binary search program for C# and write algorithm and explain it in easy words also show output and input
Write a program in C language that uses a binary search algorithm to guess a number...
Write a program in C language that uses a binary search algorithm to guess a number from 1 to 100. The computer will keep guessing until they get the users number correct.
Write a program to implement Apriori Algorithm on web log data?   do a google search for...
Write a program to implement Apriori Algorithm on web log data?   do a google search for any keyword and store the results in a file or take some web log data from internet and apply apriori algorithm to get a meaningful conclusion from the data
Correct this Binary Search (C++) // This program demostrates linear search algorithm #include <iostream> using namespace...
Correct this Binary Search (C++) // This program demostrates linear search algorithm #include <iostream> using namespace std; // Binary search algorith // f is the first , l is the last , t is the target int binarySearch(int stgrade[], int f, int l, int t) { while (f <= l) { int m = f + (l - l) / 2; // Check if x is present at mid if (stgrade[m] == t) return m; // If x greater, ignore...
Write a version of the binary search algorithm that can be used to search a string...
Write a version of the binary search algorithm that can be used to search a string vector object. Also, write a program to test your algorithm. (Use the selection sort algorithm you developed in Programming Exercise 12 to sort the vector.) Your program should prompt the user to input a series of strings, ending the input stream with zzz. The program should then prompt the user to search for a specific string in the list. If the string is found...
Write a program of Binary Search in C++ by using function and arrays with the explanation.
Write a program of Binary Search in C++ by using function and arrays with the explanation.
Binary Search Algorithm a.) Create a Java application that utilizes the "Binary Search Algorithm" presented in...
Binary Search Algorithm a.) Create a Java application that utilizes the "Binary Search Algorithm" presented in chapter 19 (NOT Java's pre-built binarySearch() method from imported Java library) to search for an integer in a random array of size 30 in the range of 0 to 1000 inclusive. You should use Java's random number generator to randomize the numbers in your array. b.) The application's main() method should display unsorted array and sorted array, prompt user for a search key, allow...
CODE IN C++ PLEASE Write a program to implement the algorithm that you designed in Exercise...
CODE IN C++ PLEASE Write a program to implement the algorithm that you designed in Exercise 19 of Chapter 1. Your program should allow the user to buy as many items as the user desires. Instructions for Exercise 19 of Chapter 1 have been posted below for your convenience. Exercise 19 Jason typically uses the Internet to buy various items. If the total cost of the items ordered, at one time, is $200 or more, then the shipping and handling...
Assume that you want to implement binary search with a linked list. What would be the...
Assume that you want to implement binary search with a linked list. What would be the performance of this algorithm? Compare and contrast this algorithm with the implementation of binary search on traditional sorted array and give a discussion. Your discussion and analysis must explain what the possibilities, issues and consequences of such design are, and then explain whether these issues would exist in the traditional array approach. Your answer can be around 1-2 paragraph of writing backed-up with algorithmic...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT