Binary Search Program in C | C++ | Java | Python

Binary search program in c without using the function

Solution In C


Code:

#include<stdio.h>
int main()
{
    int size;
    printf("Enter size of the array:");
    scanf("%d",&size);
    int arr[size],i;
    printf("Enter the element of the array:");
    for(i=0;i<size;i++)
        scanf("%d",&arr[i]);
    printf("Enter the search element:");
    int search_elm;
    scanf("%d",&search_elm);
    int found=0;
    
 int lowerBound = 0;
 int upperBound = size-1;
 int mid;
do{
mid = (lowerBound + upperBound ) / 2;
if(arr[mid]==search_elm)
found=1;
else if(lowerBound > upperBound)
found=1;
else
{
if(arr[mid] < search_elm)
lowerBound = mid + 1;
else
upperBound = mid - 1;
}
}while(search_elm!=arr[mid] && lowerBound<=upperBound);

    if(found==1)
        printf("search element is found.");
    else
        printf("search element is not found.");
}


Input/Output:
Enter size of the array:5
Enter the element of the array:10
12
7
8
9
Enter the search element:7
Search element is found.

Binary Search Program in C plus plus

Solution In C++


Code:

#include<iostream>
using namespace std;
int main()
{
    int size;
    cout<<"Enter size of the array:";
    cin>>size;
    int arr[size],i;

    cout<<"Enter the element of the array:";
    for(i=0;i<size;i++)
        cin>>arr[i];

    cout<<"Enter the search element:";
    int search_elm;
    cin>>search_elm;
    int found=0;

 int lowerBound = 0;
 int upperBound = size-1;
 int mid;

do{
mid = (lowerBound + upperBound ) / 2;
if(arr[mid]==search_elm)
found=1;
else if(lowerBound > upperBound)
found=1;
else
{
if(arr[mid] < search_elm)
lowerBound = mid + 1;
else
upperBound = mid - 1;
}
}while(search_elm!=arr[mid] && lowerBound<=upperBound);

    if(found==1)
        cout<<"Search element is found.";
    else
        cout<<"Search element is not found.";
}

Input/Output:
Enter size of the array:4
Enter the element of the array:10
12
13
16
Enter the search element:17
Search element is not found.

Binary search program in java using scanner class


Solution In Java

Code:

import java.util.Scanner;
public class Binary_Search {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int size;
    System.out.println("Enter size of the array:");
    size=cs.nextInt();
    int arr[]=new int[size],i;
    System.out.println("Enter the element of the array:");
    for(i=0;i<size;i++)
        arr[i]=cs.nextInt();
    System.out.println("Enter the search element:");
    int search_elm;
    search_elm=cs.nextInt();
    int found=0;
    int lowerBound = 0;
    int upperBound = size-1;
    int mid;
   do{
   mid = (lowerBound + upperBound ) / 2;
   if(arr[mid]==search_elm)
   found=1;
   else if(lowerBound > upperBound)
   found=1;
   else
   {
   if(arr[mid] < search_elm)
   lowerBound = mid + 1;
   else
   upperBound = mid - 1;
   }
   }while(search_elm!=arr[mid] && lowerBound<=upperBound);
        
   if(found==1)
    System.out.println("Search element is found.");
    else
    System.out.println("Search element is not found.");
    cs.close();
}
}

Input/Output:
Enter size of the array:
5
Enter the element of the array:
6
8
9
10
11
Enter the search element:
10
Search element is found.

Binary search program in python using list

Solution In Python

arr=[]
size = int(input("Enter the size of the array: "))
print("Enter the Element of the array:")
for i in range(0,size):
    num = int(input())
    arr.append(num)

search_elm=int(input("Enter the search element: "))
found=0

lowerBound = 0
upperBound = size-1

while lowerBound<=upperBound and not found:
    mid = (lowerBound + upperBound ) // 2
    if arr[mid]==search_elm:
        found=1
    else:
        if arr[mid] < search_elm:
            lowerBound = mid + 1
        else:
            upperBound = mid - 1

if found==1:
        print("Search element is found.")
else:
        print("Search element is not found.")

Input/Output:
Enter the size of the array: 5
Enter the Element of the array:
7
9
10
23
56
Enter the search element: 30
Search element is not found.

More:-

C/C++/Java/Python Practice Question 


Post a Comment

0 Comments