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))

5
2

                                    //it is using divide and conquer method
#include &lt;iostream&gt;

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

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

    return 0;
}

5 (2 Votes)
0
4.25
4

                                    
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(&quot;enter the key:&quot;);
		int key=scan.nextInt();
		int flag=0;
		int low=0;
		int high=x.length-1;
		int mid=0;
		while(low&lt;=high)
		{
			mid=(low+high)/2;
			if(key&lt;x[mid])
			{
				high=mid-1;
			}
			else if(key&gt;x[mid])
			{
				low=mid+1;
			}
			else if(key==x[mid])
			{
				flag++;
				System.out.println(&quot;found at index:&quot;+mid);
				break;
			}
		}
		if(flag==0)
		{
			System.out.println(&quot;Not found&quot;);
		}
		

	}

}

4.25 (4 Votes)
0
4
4
Tiny Posers 115 points

                                    function binarySearchRicorsivo(array A, int p, int r, int v)
    if p &gt; r
      return -1
     if v &lt; A[p] or v &gt; A[r]
       return -1
     q= (p+r)/2
     if A[q] == v
       return q
     else if A[q] &gt; v
       return binarySearchRicorsivo(A,p,q-1,v)
     else
     

4 (4 Votes)
0
0
0

                                    //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&lt;=end)
		{
			mid=(first+end)/2;
			if(key&lt;a[mid])				// Move to left part if key is smaller than middle element.
			{
				end = mid-1;
			}
			else if(key&gt;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(&quot;Success! found&quot;);
		}
		else
		{
			System.out.println(&quot;Error! This key (&quot; + key + &quot;) does not exist in the array&quot;);
		}
		
	}

}

0
0
4
8
Bye 80 points

                                    #include &lt;bits/stdc++.h&gt;
using namespace std;

int main(){
    int n;
    cin&gt;&gt;n;
    vector&lt;int&gt;v(n);
    for(int i = 0; i&lt;n; i++){
        cin&gt;&gt;v[i];
    }
    int to_find;
    cin&gt;&gt;to_find;
    int lo = 0 , hi = n-1 , mid ;
    while(hi - lo &gt; 1){
        int mid = (hi + lo)/2;
        if(v[mid] &lt; to_find){
            lo = mid + 1;
        }else{
            hi = mid;
        }
    }
    if(v[lo] == to_find){
        cout&lt;&lt;lo&lt;&lt;endl;
    }else if(v[hi] == to_find){
        cout&lt;&lt;hi&lt;&lt;endl;
    }else{
        cout&lt;&lt;&quot;Not Found&quot;; 
    }

    return 0;
}

4 (8 Votes)
0
4.71
7

                                    def binarySearch(arr, k, low, high):   
    while low &lt;= high:
        mid = low + (high - low)//2
        if arr[mid] == k:
            return mid
        elif arr[mid] &lt; k:

            low = mid + 1
        else:

            high = mid - 1
    return -1

arr = [1, 3, 5, 7, 9]

k = 5
result = binarySearch(arr, k, 0, len(arr)-1)

if result != -1:

    print(&quot;Element is present at index &quot; + str(result))

else:

    print(&quot;Not found&quot;)

4.71 (7 Votes)
0
4.57
7
Luoiliembac 115 points

                                    10 101 61 126 217 2876 6127 39162 98126 712687 1000000000100 6127 1 61 200 -10000 1 217 10000 1000000000

4.57 (7 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
Explain Binary Search binary. search what is in binary search binaryy search binaryy searching binary searchings whats a binary search algorithm &quot; binary search algorithm&quot; binary seaarch binary search' binary search basic binary search function online binary search search binary Binary Search. binary search criteria About binary search binary search On binary search start how to do a binary search of Binary Search? \ binary search binary search o(?) basic binary search binary search programmin binaryu search binary search arrays binary search array binary serarch binary search,com binary search com\ binary seachr what is binary search.com define binary search binary Search. com binary search uses Explain binary search. bin search binary searching algorithm binary searchstl binary search for binary search search code binary search how to use binary search binary search O binry search whats a binary search binary search lookup standard binary search binary search i binary Search u binary search sekvesial search is binary search algorithm binary search order binary search by name what does binary search return how does a binary search work what in binary search binary search implementaion implement binary search binary search ..com binary search analysis binary searchc# binary search meaning binay search who invented the binary search binary search com how a binary search works? binary search o( binary serach on binary search works binary search algorithm binary search] Binary Search Algorithm: search in binary search rtee what is the binary search what is binary search used for binary search O() binary search wiki binary search .com binary search example binary search explained binary searching binary search program how to implement binary search How does a binary search work? is a binary search an algorithm binary search online binary serch Explain Binary search method with an example. binary learch algorithm for binary search binary search mid l - 1 r + 1 binary search code hwo to do binary search function binary sort binary search on answer binary search visualization binary search questions binary search for strings when to use binary search binary search algorith, how binary sort works binary seatch binary search and linear search binary search leetcode binary search steps algorithm of binary search binary search simple definition binary search examples BINARY SEARCH: Write a recursive function that searches for a target in a sorted array using binay search, where the array, its size and the target are given as parameters. what is binery search binary algorithm binary search.com what is a binary search binary search vs sort c++ to search element using binary search Binary search algorithm. math defintion for binary search function binary search nororal questions on log n formula binary search oral questions on log n formula algorithm binary search binary search explanation What is Binary Search? binary sort efficiency what is binary search binary search how is it used binary search mcq ordinary search algorithm binary search tutorial binarz search the process of binary search can be designed well using what is a binary search algorithm binary search example java binary search in java what is binary algorithm binary seracg binary search space complexity c bnary search logarithmic search Binary search algorthm binary search re purpose of binary search binary search process sorting binary search Binary search method Binary search algorithm bineary search binary search algorthim the binary search algorithm algorithm for binary dearch binary search sort code binary search example binarysearch.com binairy search binary serach algorithm binary searc binary search tee binary algoridim binary seacrch java binary seach what is binary serach what is the binary search equation? binayr search how does binary search work binary dsearch binary search\ binary search example graphic binary search ordered list binarty search binary search binary search while loop binary search in array bineary search function on half sorted arrry how to do binary search binaray search binary search is INARY SEARCH binary seacrh binary serach what is a binary search? Binary Search- binarry search binary search order explanation binary search definition nbinary search java binary search on sorted array binarysearch coding platform divide and conquer search sorted array binary search logic binary search
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source