binary search

Solutions on MaxInterview for binary search by the best coders in the world

showing results for - "binary search"
Ana
17 Apr 2017
1#binary search python
2def binaryy(ar, ele):
3    low = 0 
4    high = len(ar)-1
5    if ele not in ar:
6        return "Not Found"
7    while low <= high:
8        mid = (low + high) // 2
9        if ar[mid] < ele:
10            low = mid + 1
11        elif ar[mid] > ele:
12            high = mid - 1
13        else:
14            return mid
15
16
17ar = [10, 20, 30, 40, 50]
18ele = 55
19print(binaryy(ar, ele))
Neyla
29 Jul 2020
1#include <bits/stdc++.h>
2using namespace std;
3
4int main(){
5    int n;
6    cin>>n;
7    vector<int>v(n);
8    for(int i = 0; i<n; i++){
9        cin>>v[i];
10    }
11    int to_find;
12    cin>>to_find;
13    int lo = 0 , hi = n-1 , mid ;
14    while(hi - lo > 1){
15        int mid = (hi + lo)/2;
16        if(v[mid] < to_find){
17            lo = mid + 1;
18        }else{
19            hi = mid;
20        }
21    }
22    if(v[lo] == to_find){
23        cout<<lo<<endl;
24    }else if(v[hi] == to_find){
25        cout<<hi<<endl;
26    }else{
27        cout<<"Not Found"; 
28    }
29
30    return 0;
31}
Jonah
18 Mar 2019
1
2import java.util.Scanner;
3
4public class Binarysearch {
5
6	public static void main(String[] args) {
7		int[] x= {1,2,3,4,5,6,7,8,9,10,16,18,20,21};
8		Scanner scan=new Scanner(System.in);
9		System.out.println("enter the key:");
10		int key=scan.nextInt();
11		int flag=0;
12		int low=0;
13		int high=x.length-1;
14		int mid=0;
15		while(low<=high)
16		{
17			mid=(low+high)/2;
18			if(key<x[mid])
19			{
20				high=mid-1;
21			}
22			else if(key>x[mid])
23			{
24				low=mid+1;
25			}
26			else if(key==x[mid])
27			{
28				flag++;
29				System.out.println("found at index:"+mid);
30				break;
31			}
32		}
33		if(flag==0)
34		{
35			System.out.println("Not found");
36		}
37		
38
39	}
40
41}
42
Ana
11 Sep 2017
1//Binary search can apply to sorted data only.
2//Time complexity of binary search is O(log n ).
3//It always divide the whole data in parts and compare  a search key to middle element only.
4
5
6import java.util.*;
7public class BinarySearch {
8
9	public static void main(String[] args) {
10		// TODO Auto-generated method stub
11		Scanner sc = new Scanner(System.in);
12		int[] a = {10,20,50,30,40};
13		int key=sc.nextInt();
14		
15		Arrays.sort(a);					// An method in java.util.Arrays package to sort an array element.
16		
17		int first=0,end=a.length-1,mid=0,flag=0;
18
19		while(first<=end)
20		{
21			mid=(first+end)/2;
22			if(key<a[mid])				// Move to left part if key is smaller than middle element.
23			{
24				end = mid-1;
25			}
26			else if(key>a[mid])		   // Move to right part if key is greater than middle element.
27			{
28				first = mid+1;
29			}
30			else
31			{
32				flag=1;
33				break;
34			}
35		}
36		if(flag==1)
37		{
38			System.out.println("Success! found");
39		}
40		else
41		{
42			System.out.println("Error! This key (" + key + ") does not exist in the array");
43		}
44		
45	}
46
47}
48
Clarabelle
03 Jan 2020
1a = "kartik"
2print ("a")
Ilaria
10 Apr 2020
1function binarySearchRicorsivo(array A, int p, int r, int v)
2    if p > r
3      return -1
4     if v < A[p] or v > A[r]
5       return -1
6     q= (p+r)/2
7     if A[q] == v
8       return q
9     else if A[q] > v
10       return binarySearchRicorsivo(A,p,q-1,v)
11     else
12     
Jarvis
07 Feb 2019
110 101 61 126 217 2876 6127 39162 98126 712687 1000000000100 6127 1 61 200 -10000 1 217 10000 1000000000
Ethann
12 Oct 2018
1def binarySearch(arr, k, low, high):   
2    while low <= high:
3        mid = low + (high - low)//2
4        if arr[mid] == k:
5            return mid
6        elif arr[mid] < k:
7
8            low = mid + 1
9        else:
10
11            high = mid - 1
12    return -1
13
14arr = [1, 3, 5, 7, 9]
15
16k = 5
17result = binarySearch(arr, k, 0, len(arr)-1)
18
19if result != -1:
20
21    print("Element is present at index " + str(result))
22
23else:
24
25    print("Not found")
26
Diego Alejandro
26 Apr 2019
1//it is using divide and conquer method
2#include <iostream>
3
4using namespace std;
5void binarysearch(int arr[],int start,int end,int val)
6{
7    if(start<end)
8    {
9        int mid=(start+end)/2;
10        if(arr[mid]==val)
11        {
12            cout<<"value found:"<<endl;
13        }
14        else if(arr[mid]<val)
15        {
16            binarysearch(arr,mid+1,end,val);
17        }
18        else if(arr[mid]>val)
19        {
20            binarysearch(arr,start,mid-1,val);
21        }
22    }
23    else
24    {
25        cout<<"not present:"<<endl;
26    }
27}
28
29int main()
30{
31    int n;
32    cout<<"enter the size of the array:"<<endl;
33    cin>>n;
34    int arr[n];
35    cout<<"enter the elements of the array:"<<endl;
36    for(int i=0;i<n;i++)
37    {
38        cin>>arr[i];
39    }
40    cout<<"enter the value you want to search:"<<endl;
41    int val;
42    cin>>val;
43    binarysearch(arr,0,n-1,val);
44
45    return 0;
46}
47
similar questions
queries leading to this page
binary search