
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Implement a Binary Search Algorithm for a Specific Search Sequence
In this Program we need to implement binary search to find the existence of a search sequence in an array. The time complexity of Binary search is O(log(n)).
Required steps and pseudocodes
Begin BinarySearch() function has ‘arr’ the array of data and ‘n’ the number of values, start and end index, iteration count and b[0] be the element to be searched in the argument list. Increment the iteration counter and compare the item value with the a[mid]. If item < a[mid] choose first half otherwise second half to proceed further. Return index value to main. In main(), sequentially compare the remaining items of search sequence to next items in the array. Print the index range of the sequence found. End.
Example Code
#include<iostream> using namespace std; int BinarySearch(int a[], int start, int end, int item, int iter) { int i, mid; iter++; mid = start+ (end-start+1)/2; if(item > a[end] || item < a[start] || mid == end) { cout<<"\nNot found"; return -1; } else if(item == a[mid]) { return mid; } else if(item == a[start]) { return start; } else if(item == a[end]) { return end; } else if(item > a[mid]) BinarySearch(a, mid, end, item, iter); else BinarySearch(a, start, mid, item, iter); } int main() { int n, i, flag=0, Bin, len = 9, a[10]={1, 7, 15, 26, 29, 35, 38, 40, 49, 51}; cout<<"\nEnter the number of element in the search sequence: "; cin>>n; int b[n]; for(i = 0; i < n; i++) { cin>>b[i]; } Bin = BinarySearch(a, 0, len, b[0], 0); if (Bin == -1) { cout<<"\nNot found."; return 0; } else { for(i = Bin; i < n+Bin; i++) if(a[i] != b[i-Bin]) flag = 4; if(flag == 4) cout<<"\nNot found."; else cout<<"\nSequence found between index "<<Bin<<" and "<<Bin+n<<"."; } return 0; }
Output
Enter the number of element in the search sequence: 4 15 26 29 35 Sequence found between index 2 and 6.
- Related Articles
- C++ Program to Implement Interpolation Search Algorithm
- Java program to implement binary search
- C++ Program to Implement the String Search Algorithm for Short Text Sizes
- C++ Program to Search for an Element in a Binary Search Tree
- C++ Program to Implement Randomized Binary Search Tree
- Python Program to Implement Binary Search without Recursion
- Python Program to Implement Binary Search with Recursion
- C++ Program to Implement a Binary Search Tree using Linked Lists
- Python Program for Binary Search
- 8085 program for Binary search
- Java Program to implement Binary Search on char array
- Java Program to implement Binary Search on long array
- Java Program to implement Binary Search on double array
- Java Program to implement Binary Search on float array
- Java Program to implement Binary Search on an array

Advertisements