Answers for "how to implement binary search"

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
1

binary search iterative

// Binary Search using Iterative Approach

import java.io.*;
class Binary_Search
{
	public static void main(String[] args) throws Exception
	{
		Binary_Search obj = new Binary_Search();
		InputStreamReader isr = new InputStreamReader(System.in);
		BufferedReader br = new BufferedReader(isr);
		System.out.println("Insert the length of the Array : ");
		int n = Integer.parseInt(br.readLine());
		int arr[] = new int[n];
		System.out.println("Insert elements into the array : ");
		for(int i=0;i<n;i++)
		{
			arr[i] = Integer.parseInt(br.readLine());
		}
		System.out.println("Enter the num which you want to Search : ");
		int num = Integer.parseInt(br.readLine());
		obj.logic(arr,num);
	}
	void logic(int arr[],int num)
	{
		int r = arr.length - 1;
		int l = 0;
		int mid;
		while(l<=r)
		{
			mid = l + (r-l)/2;
			if(arr[mid] == num)
			{
				System.out.println("Number found at "+mid+"th index");
				break;
			}
			else if(arr[mid]>num)
			{
				r = mid - 1;
			}
			else
			{
				l = mid + 1;
			}
		}
	}
}
Posted by: Guest on June-23-2020
0

binary search algorithm

#include <bits/stdc++.h>

using namespace std;

int binarySearch(int arr[], int l, int h, int key){
    if(l<=h){
        int mid = l + (h-l)/2;

        if(arr[mid] == key){
            return mid;
        }

        else if(arr[mid] > key){
            return binarySearch(arr, l, mid-1, key);
        }

        else if(arr[mid] < key){
            return binarySearch(arr,mid+1, h, key);
        }
    }       

    return -1;
}

int main(){
    int arr[] = {1,2,3,4,5,6,7,8,9,10};
    int n = sizeof(arr)/sizeof(arr[0]);
    int key = 7;

    int result = binarySearch(arr,0,n-1,key);

    (result==-1)
        ? cout << "Element is not found in the array" << endl
        : cout << "Element is found at index " << result;

    return 0;

}
Posted by: Guest on January-16-2021
0

binary search

//it is using divide and conquer method
#include <iostream>

using namespace std;
void binarysearch(int arr[],int start,int end,int val)
{
    if(start<end)
    {
        int mid=(start+end)/2;
        if(arr[mid]==val)
        {
            cout<<"value found:"<<endl;
        }
        else if(arr[mid]<val)
        {
            binarysearch(arr,mid+1,end,val);
        }
        else if(arr[mid]>val)
        {
            binarysearch(arr,start,mid-1,val);
        }
    }
    else
    {
        cout<<"not present:"<<endl;
    }
}

int main()
{
    int n;
    cout<<"enter the size of the array:"<<endl;
    cin>>n;
    int arr[n];
    cout<<"enter the elements of the array:"<<endl;
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    cout<<"enter the value you want to search:"<<endl;
    int val;
    cin>>val;
    binarysearch(arr,0,n-1,val);

    return 0;
}
Posted by: Guest on May-27-2021

Code answers related to "how to implement binary search"

Browse Popular Code Answers by Language