VSDevelopers, Algorithms Coliseum

Binary Search Tree

/*Copyrights to vsdevelopers.io*/
/*For more programs visit vsdevelopers.io */
/*Java program to find the element in an array using Binary serach*/
import java.util.Scanner;

public class VSDBinarySearch {
static Scanner sc = new Scanner(System.in);
static int n;// Size of array
static int arr[];
static int f;// Element to be serached
// Function to take userinput

public static void VSDuserInput() {
System.out.println("Enter size");
n = sc.nextInt();
arr = new int[n];
System.out.println("Enter array elements");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
System.out.println("enter search element");
f = sc.nextInt();


// Function to perform Binary search
public static int VSDbinarySearch(int l, int r) {
if (l <= r) {
int mid = l + (r - l) / 2;
if (arr[mid] == f)
return mid;
else if (arr[mid] < f) 
VSDbinarySearch(l, mid - 1); 
else if (arr[mid] > f)
VSDbinarySearch(mid + 1, r);

return -1;

public static void main(String[] args) {
int n = VSDbinarySearch(0, arr.length);
if (n == -1)
System.out.println("The given element is not found");
System.out.println("The given element is found at index: " + n);

# program to find an element in the given sorted array using binary search

# function takes array,start index,end index and element to be found as parameters
def binary_search(array,start,end,element):
if start <= end and start >= 0 :
mid = start + (end - start)//2

if array[mid] == element:
print(f&quot;The element {element} found at index {mid} in the given array&quot;)

if element < array[mid] :
return binary_search(array,start,mid-1,element)

return binary_search(array,mid+1,end,element)
print(f&quot;The element {element} not found in the given array&quot;)

# Test drive code:
n=int(input(&quot;Enter no of elements in array : &quot;))
array=[int(x) for x in input(f&quot;Enter {n} elements in to the array: &quot;).split()]
element= int(input(&quot;Enter element to find in the array: &quot;))