Answers for "implement binary search"

14

binary search program c++

#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;
}
Posted by: Guest on October-31-2020
12

binary search java

// Java implementation of iterative Binary Search 
class BinarySearch { 
	// Returns index of x if it is present in arr[], 
	// else return -1 
	int binarySearch(int arr[], int x) 
	{ 
		int l = 0, r = arr.length - 1; 
		while (l <= r) { 
			int m = l + (r - l) / 2; 

			// Check if x is present at mid 
			if (arr[m] == x) 
				return m; 

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

			// If x is smaller, ignore right half 
			else
				r = m - 1; 
		} 

		// if we reach here, then element was 
		// not present 
		return -1; 
	} 

	// Driver method to test above 
	public static void main(String args[]) 
	{ 
		BinarySearch ob = new BinarySearch(); 
		int arr[] = { 2, 3, 4, 10, 40 }; 
		int n = arr.length; 
		int x = 10; 
		int result = ob.binarySearch(arr, x); 
		if (result == -1) 
			System.out.println("Element not present"); 
		else
			System.out.println("Element found at "
							+ "index " + result); 
	} 
}
Posted by: Guest on April-02-2020
2

binary search java

binary search program in java.
public class BinarySearchExample
{
   public static void binarySearch(int[] arrNumbers, int start, int end, int keyElement)
   {
      int middle = (start + end) / 2;
      while(start <= end)
      {
         if(arrNumbers[middle] < keyElement)
         {
            start = middle + 1;
         }
         else if(arrNumbers[middle] == keyElement)
         {
            System.out.println("Element found at index: " + middle);
            break;
         }
         else
         {
            end = middle - 1;
         }
         middle = (start + end) / 2;
      }
      if(start > end)
      {
         System.out.println("Element not found!");
      }
   }
   public static void main(String[] args)
   {
      int[] arrNumbers = {14,15,16,17,18};
      int keyElement = 16;
      int end = arrNumbers.length - 1;
      binarySearch(arrNumbers, 0, end, keyElement);
   }
}
Posted by: Guest on October-23-2020
8

binary search

#binary search python
def binaryy(ar, ele):
    low = 0 
    high = len(ar)-1
    if ele not in ar:
        return "Not Found"
    while low <= high:
        mid = (low + high) // 2
        if ar[mid] < ele:
            low = mid + 1
        elif ar[mid] > ele:
            high = mid - 1
        else:
            return mid


ar = [10, 20, 30, 40, 50]
ele = 55
print(binaryy(ar, ele))
Posted by: Guest on July-24-2021
1

binary search

import java.util.Scanner;

public class Binarysearch {

	public static void main(String[] args) {
		int[] x= {1,2,3,4,5,6,7,8,9,10,16,18,20,21};
		Scanner scan=new Scanner(System.in);
		System.out.println("enter the key:");
		int key=scan.nextInt();
		int flag=0;
		int low=0;
		int high=x.length-1;
		int mid=0;
		while(low<=high)
		{
			mid=(low+high)/2;
			if(key<x[mid])
			{
				high=mid-1;
			}
			else if(key>x[mid])
			{
				low=mid+1;
			}
			else if(key==x[mid])
			{
				flag++;
				System.out.println("found at index:"+mid);
				break;
			}
		}
		if(flag==0)
		{
			System.out.println("Not found");
		}
		

	}

}
Posted by: Guest on August-03-2020
0

binary search

//Binary search can apply to sorted data only.
//Time complexity of binary search is O(log n ).
//It always divide the whole data in parts and compare  a search key to middle element only.


import java.util.*;
public class BinarySearch {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int[] a = {10,20,50,30,40};
		int key=sc.nextInt();
		
		Arrays.sort(a);					// An method in java.util.Arrays package to sort an array element.
		
		int first=0,end=a.length-1,mid=0,flag=0;

		while(first<=end)
		{
			mid=(first+end)/2;
			if(key<a[mid])				// Move to left part if key is smaller than middle element.
			{
				end = mid-1;
			}
			else if(key>a[mid])		   // Move to right part if key is greater than middle element.
			{
				first = mid+1;
			}
			else
			{
				flag=1;
				break;
			}
		}
		if(flag==1)
		{
			System.out.println("Success! found");
		}
		else
		{
			System.out.println("Error! This key (" + key + ") does not exist in the array");
		}
		
	}

}
Posted by: Guest on January-30-2021

Code answers related to "implement binary search"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language