Question
in c++
Class Assignment Nov-1: Write the binary search in template form.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Binary Search template in CPP - binary_search() is a function defined in the standard template library of c++. We will use this function to implement binary search.

We will use "algorithm" , C++ standard library.

Syntax-

binary_search(S_A,E_A,data)

S_A=First element pointer

E_A=Last element pointer

data=element to search.

----------------------------------------------------------------------------------------------------------------------------------------------------------------------Code-

#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
int n,element; //element to take input size o array, element=element to search
cout<<"Enter size of the array : ";
cin>>n;

int arr[n]; //declaring array of size n
cout<<"\nEnter array elements : ";
for(int i = 0; i < n; i++)
cin >> arr[i];

//used in built sort func
sort(arr, arr+n);

cout<<"\nsorted elements are : ";
for(int i = 0; i < n; i++)
cout <<" "<< arr[i];

cout<<"\nEnter element to search : ";
cin>>element;

if(binary_search(arr, arr+ n, element))
cout<<"\npresent in array\n";
else
cout<<"\nnot present in array\n";

return 0;
}
----------------------------------------------------------------------

Code Screenshot-

CUsers Shradhda\Documents binary.cpp - Sublime Text File Edit Selection Find View Goto Tools Project Preferences Help binary-------------------------------------------------------------------------------------------

Output Screenshot-

ols Film 1 #include <iostream> C:\Users\Shradhda\Documents\binary.exe Enter size of the array : 5 Enter array elements : 4 6

Add a comment
Know the answer?
Add Answer to:
in c++ Class Assignment Nov-1: Write the binary search in template form.
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
  • Binary Tree Template Write your own version of a class template that will create a binary...

    Binary Tree Template Write your own version of a class template that will create a binary tree that can hold values of any data type. Demonstrate the class with a driver program. Place your binary tree template in it's own header file, Btree.h. Include methods for the following: inserting new values into the tree removing nodes from the tree searching the tree returning the number of nodes in the tree displaying the contents of the tree using preorder traversal Your...

  • write a c++ program that conducts a binary search using a template in: a set of...

    write a c++ program that conducts a binary search using a template in: a set of values of type int, a set of values of type float, a set of values of type double and a set of values of type char. Assume that the array may have up to 10 values.

  • Consider the class specifications for the Binary Tree class and Binary Search Tree class in the...

    Consider the class specifications for the Binary Tree class and Binary Search Tree class in the attached files // BinaryTree.h #include <iostream> using namespace std; //Definition of the Node template <class elemType> struct TreeNode { elemType data; TreeNode<elemType> *left; TreeNode<elemType> *right; }; //Definition of class Binary Tree template <class elemType> class BinaryTree { protected: TreeNode<elemType> *root; public: BinaryTree(); BinaryTreel const BinaryTree<elemType>& otherTree); BinaryTree(); bool is Empty() const; virtual boot search(const elemType& searchItem) const = 0; virtual void insert(const elemType& insertItem)...

  • LANGUAGE: C++ Write a class to create the binary tree (insert, delete, search, exit) and display...

    LANGUAGE: C++ Write a class to create the binary tree (insert, delete, search, exit) and display the output using inorder, preorder and postorder tree traversal methods.

  • (Recursive Binary Search) Write a recursive method recursiveBinarySearch to perform a binary search of an array....

    (Recursive Binary Search) Write a recursive method recursiveBinarySearch to perform a binary search of an array. The method should receive the search key, starting index and ending index as arguments. If the search key is found, return its index in the array. If the search key is not found, return –1. (NOTE: Complete the recursiveBinarySearch method in the BinaryArray class). java

  • Consider the class specifications for the Binary Tree class and BinarySearch Tree class below: // Binary...

    Consider the class specifications for the Binary Tree class and BinarySearch Tree class below: // Binary Tree.h #include <iostream> using namespace std; //Definition of the Node template <class elemType struct TreeNode { elemType data; TreeNode<elemType> *left; TreeNode<elemType *right; }; //Definition of class Binary Tree template <class elemType> class Binary Tree { protected: TreeNode<elemType> *root; public: BinaryTree(); BinaryTreel const BinaryTree<elemType>& otherTree); -Binary Tree(): bool is Empty() const; virtual bool search(const elemTypes searchItem) const = 0; virtual void insert(const elemTypek insertItem) =...

  • write in C++ polynomial class that the data type of the coefficients is a template parameter....

    write in C++ polynomial class that the data type of the coefficients is a template parameter. This data type can be any type that has operators for addition, subtraction multiplication and assignment. The class should have a default constructor which results in a zero value. For example our template class would allow us to build polynomails where coefficients are complex numbers ( using the complex<double> type < complex>)

  • Consider the partial implementation of a Binary Search Tree (BST) class. For simplicity, each Node stores...

    Consider the partial implementation of a Binary Search Tree (BST) class. For simplicity, each Node stores only the key. Add a public member function to class BST that returns the largest absolute value in the tree. The language is C++ Want the height #4 Coding [6 points] Consider the partial implementation of a Binary Search Tree (BST) class. For simplicity, each Node stores only the key. Add a public member function to class BST that returns the height of the...

  • ***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 python 11.1 Binary Search Tree In this assignment, you will implement a Binary Search Tree...

    in python 11.1 Binary Search Tree In this assignment, you will implement a Binary Search Tree You will also need to implement a Node class. This class will not be tested, but is needed to implement the BST. Your BST must implement the following methods. You are free to implement additional helper methods. It is recommended you create your own helper methods Constructor: Creates an Empty Tree String Method: Returns the string "Empty Tree" for an empty tree. Otherwise, returns...

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
ADVERTISEMENT