"binary search program c++" Code Answer's

You're definitely familiar with the best coding language C++ that developers use to develop their projects and they get all their queries like "binary search program c++" answered properly. Developers are finding an appropriate answer about binary search program c++ related to the C++ coding language. By visiting this online portal developers get answers concerning C++ codes question like binary search program c++. Enter your desired code related query in the search bar and get every piece of information about C++ code related question on binary search program c++. 

binary search program c++

By CodeMyFingahCodeMyFingah on Oct 31, 2020
#include <iostream>
using namespace std;

// This program performs a binary search through an array, must be sorted to work
int binarySearch(int array[], int size, int value) 
{   
    int first = 0,         // First array element       
    last = size - 1,       // Last array element       
    middle,                // Mid point of search       
    position = -1;         // Position of search value   
    bool found = false;        // Flag   
    while (!found && first <= last) 
    {      
        middle = (first + last) / 2;     // Calculate mid point      
        if (array[middle] == value)      // If value is found at mid      
    	{         
                found = true;         
                position = middle;      
        }      
        else if (array[middle] > value)  // If value is in lower half         
            last = middle - 1;      
        else         
            first = middle + 1;          // If value is in upper half   
    }   
    return position;
}
int main ()
{
    const int size = 5; // size initialization
    int array[size] = {1, 2, 3, 4, 5}; // declare array of size 10
    int value; // declare value to be searched for
    int result; // declare variable that will be returned after binary search

    cout << "What value would you like to search for? "; // prompt user to enter value
    cin >> value;
    result = binarySearch(array, size, value);

    if (result == -1) // if value isn't found display this message
        cout << "Not found\n";
    else  // If value is found, displays message
        cout << "Your value is in the array.\n"; 
  
    return 0;
}

Add Comment

10

binary search in c++

By Helpful_HeroinHelpful_Heroin on Jul 20, 2020
#include<iostream> 
using namespace std; 
int binarySearch(int arr[], int p, int r, int num) { 
   if (p <= r) { 
      int mid = (p + r)/2; 
      if (arr[mid] == num)   
         return mid ; 
      if (arr[mid] > num)  
         return binarySearch(arr, p, mid-1, num);            
      if (arr[mid] < num)
         return binarySearch(arr, mid+1, r, num); 
   } 
   return -1; 
} 
int main(void) { 
   int arr[] = {1, 3, 7, 15, 18, 20, 25, 33, 36, 40}; 
   int n = sizeof(arr)/ sizeof(arr[0]); 
   int num = 33; 
   int index = binarySearch (arr, 0, n-1, num); 
   if(index == -1)
      cout<< num <<" is not present in the array";
   else
      cout<< num <<" is present at index "<< index <<" in the array"; 
   return 0; 
}

Add Comment

5

All those coders who are working on the C++ based application and are stuck on binary search program c++ can get a collection of related answers to their query. Programmers need to enter their query on binary search program c++ related to C++ code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about binary search program c++ for the programmers working on C++ code while coding their module. Coders are also allowed to rectify already present answers of binary search program c++ while working on the C++ language code. Developers can add up suggestions if they deem fit any other answer relating to "binary search program c++". Visit this developer's friendly online web community, CodeProZone, and get your queries like binary search program c++ resolved professionally and stay updated to the latest C++ updates. 

C++ answers related to "binary search program c++"

View All C++ queries

C++ queries related to "binary search program c++"

binary search program c++ how to do binary search in c++ using STL binary search tree in cpp using class binary search stl binary search function in c++ binary search in c++ binary tree search deletion in a binary search tree c++ binary search binary search algorithm binary search tree sorted order c++ binary search lower bound C Binary Search binary search in java Binary Search implementation binary search in c binary search in stl Write a program in C++ to find post-order predecessor of a node in a Binary Tree Write a program to sort an array 100,200,20, 75,89.198, 345,56,34,35 using Bubble Sort. The program should be able to display total number of passes used for sorted data in given data set. convert binary to decimal c++ stl binary exponentiation binary index tree c++ binary sort c++ binary addition using bitwise operators convert decimal to binary in c++ convert int to binary string c++ binary exponentiation modulo m binary indexed tree c++ display numbers as binary built in function in c++ for binary to decimal write and read string binary file c++ convert long int to binary string c++ print binary in c binary tree deletion how to do decimal to binary converdsion in c++ Decimal to binary c++ top view of binary tree c++ binary heap heap sort heapify and max heap in binary tree decimal to binary predefined function find number of 1s in a binary cv::mat image c++ vector decimal to binary Print Decimal to binary using stack is obje file binary?? how to find the left most bit 1 in binary of any number searching display insert in a binary serach tree how to show c++ binary files in sublime text binary algebra cpp building native binary with il2cpp unity vertical traversal of binary tree Print Nodes in Top View of Binary Tree search in vector of pairs c++ array search c++ ternary search c++ delete and search edge in adjacency matrix of a graph how to search integer in c++ c++ vector quick search dichotomic search c++ linear search in c bst search linear search c program to add two numbers sfml base program Name one example of a “decider” program that you regularly encounter in real life. sfml basic program c++ program for addition of two numbers using functions c++ calculator program using switch case program to calculate factorial of number in c++ polymorphism-program.cpp compile c++ program tower of hanoi program in c Write a program to find the sum of all sub-arrays of a given integer array. write a program to implement stack using array sfml default program c++ object program infix to postfix program in c++ if else program in c ++ run c++ program in mac terminal program to know if a number is prime running a c++ program in visual studio code cannot edit in read only editor Write a program to print following pattern; 1 1 2 1 2 3 1 2 3 4 insertion sort in c++ program c++ prime number program c++ program for matrix addition c++ program to reverse an array all pair shortest path algorithm in c with program c++ hello world program Write a program that inputs test scores of a student and display his grade cpp program to find average of n numbers how to end a c++ program early c++ input from terminal when program is called Application of c++ in youtube program c++ program to find gcd of 3 numbers c++ program to generate prime numbers run program until ctrl-d c++ Write a C++ program using class and objects. You have to define multiple-member functions outside class and all those functions will be the same name c++ program to input and print text using Dynamic Memory Allocation.loop "how we write a program for" time swap" in c plus plus only with string" fcfs preemptive scheduling program in c++ infix to prefix using cpp linked list program Write a c++ program to print number triangle. c ++ Program for addition of two matrix in diagonal using pointers c++ program that calculates the distance covered by a vehicle given the speed and time. menu driven program to delete in linked list how to type a vertical stack program c++ c++ program to count number of characters of words in a file using stringstream cpp program to temove space from string Write a program that inputs time in seconds and converts it into hh-mm-ss format Write a c++ program that reads a sentence (including spaces) and a word, then print out the number of occurrences of the word in the sentence write a program that simulates the rolling of two dice in c++ write a c++ program that reads ten strings and store them in array of strings, sort them and finally print the sorted strings simple program for sign in and sign up in c++ c++ program to print fibonacci series malloc c++ program set keybinding for compiling c++ program in neovim c++ program how to let the user choose different game modes Write a program to implement Liang-Bersky line clipping algorithm Create a program that finds the minimum value in these numbers bubble sort program in c++ how to run a c++ program in the background hello world program in c++ c++ how to create a program that stores details Dfs program in c++ how to make a c++ iostream program restart when finished program to swap max and min in matrix bracket balancing program in java c program to convert infix to postfix

Browse Other Code Languages

CodeProZone