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 <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;
}
Thank you!
2
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("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");
}
}
}
Thank you!
4
0
4
4
function binarySearchRicorsivo(array A, int p, int r, int v)
if p > r
return -1
if v < A[p] or v > A[r]
return -1
q= (p+r)/2
if A[q] == v
return q
else if A[q] > v
return binarySearchRicorsivo(A,p,q-1,v)
else
Thank you!
4
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<=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");
}
}
}
Thank you!
0
0
4
8
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
vector<int>v(n);
for(int i = 0; i<n; i++){
cin>>v[i];
}
int to_find;
cin>>to_find;
int lo = 0 , hi = n-1 , mid ;
while(hi - lo > 1){
int mid = (hi + lo)/2;
if(v[mid] < to_find){
lo = mid + 1;
}else{
hi = mid;
}
}
if(v[lo] == to_find){
cout<<lo<<endl;
}else if(v[hi] == to_find){
cout<<hi<<endl;
}else{
cout<<"Not Found";
}
return 0;
}
Thank you!
8
0
4.71
7
def binarySearch(arr, k, low, high):
while low <= high:
mid = low + (high - low)//2
if arr[mid] == k:
return mid
elif arr[mid] < 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("Element is present at index " + str(result))
else:
print("Not found")
Thank you!
7
0
4.57
7
10 101 61 126 217 2876 6127 39162 98126 712687 1000000000100 6127 1 61 200 -10000 1 217 10000 1000000000
Thank you!
7
0
Are there any code examples left?
New code examples in category Python