Question
Asked Mar 22, 2020
1 views

Write a program to search an element in the array.

Plz implement this using binary search.

Comments must be added .

Do it in c++

check_circle

Expert Answer

Step 1

Program Approach –

 

  • Include the essential header files.
  • Define the main function.
  • Define the variables as per the requirement.
  • Define the array and initialize the values in it.
  • Define a recursive binary search function.
  • Call the recursive binary search function.
  • Display the output.

 

       Binary Search approach -

 

  • Equate the number to be searched with the middle element of the array.
  • If the number matches with middle element of the array, then return the index number of the middle element.
  • If the number is greater than the middle element, then the number can only exist in the right half subarray after the middle element. Repeat this process for the right subarray.
  • Else the number is smaller than the middle element then the number can only exist in the left half subarray before the middle element. Repeat this process for the left sub-array.
Step 2

Code (in C++) -

#include <iostream> 
using namespace std; 
 //defining the recursive binary search function 
int bS(int a[], int len, int r, int num) 

    if (r >= len) { 
        int mid = len + (r - len) / 2; 
        //when the number is found at the middle
        if (a[mid] == num) 
            return mid; 
            //when the number is smaller than the middle element of the array,then it must be in the left sub array
        if (a[mid] > num) 
            return bS(a, len, mid - 1, num); 
            //else the element must be in the right sub array
        return bS(a, mid...

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.

Related Computer Science Q&A

Find answers to questions asked by student like you
Show more Q&A
add
question_answer

Q: Write a program in a single file that: Main: Creates 10 random doubles, all between 1 and 11, Calls ...

A: Create an object for a file. Declare an array to store the random numbers of type double. Take a var...

question_answer

Q: PART C. Write a program, ArrayRange, that asks the user to input integers and that displays the diff...

A: Programming language is missing in the question. The below program is written in Java programming la...

question_answer

Q: In c++ Write a program to calculate the simple interest. Plz add comments in the code.

A: Program approach – Include the essential headers. Define the main method. Declare the variables. Pr...

question_answer

Q: Use the library and Internet to find information about distributed systems architectures. Describe ...

A: Distributed system: The distributed system is a collection of nodes or networked computer which com...

question_answer

Q: Program to  Reverse a string by using recursion.  Plz add comments. Reply fast.

A: Program Approach -  Include the essential headers. Declare the variables as per the requirement. De...

question_answer

Q: There is a PayRoll Class that has data members for an employee’s hourly pay rate, number of hours wo...

A: Programming instructions for the revised version of program and PayRoll: Declare a variable named I...

question_answer

Q: Write a function called largest_sum_sequence() that accepts an array of signed integers and the numb...

A: The programming language is missing in the question. The below program is provided in C programming ...

question_answer

Q: question about part (B) -1.0 and (C) -0.0

A: IEE single-precision floating-point: - The IEEE 754 single-precision floating-point format uses 32 b...

question_answer

Q: A B+-tree is to be stored on disk whose block size is 2048 bytes. to be stored are 64 bytes, and the...

A: B Tree – In this binary search tree, a node can have more than 2 children and is a self-balancing tr...