Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

BINARY SEARCH

• Binary Search algorithm is used to search a sorted array by repeatedly dividing the
search interval in half. Begin with an interval covering the whole array. If the value
of the search key is less than the item in the middle of the interval, narrow the
interval to the lower half. Otherwise narrow it to the upper half. Repeatedly check
until the value is found or the interval is empty.
• IMPORTANT VARIABLE SUSED IN BINARY SEARCH
l – lower bound (represents the index of the first cell of the interval in which the search
operation is conducted)

• u- upper bound (represents the index of the last cell of the interval in which the
search operation is conducted)
• m - mid index (average of l and u. The search item is compared with the value
present at index m) . m=(l+u)/2

PROGRAM NUMBER 18
Write a program to initialize an array with the values 10,20,30,40,50,60,70,80.
Input a number and use binary search algorithm to check whether the number is
present in the array or not.
import java.util.*;
class BinarySearchAsc
{
//function to check whether a number is present
//in the array or not,using binary search algorithm.
//array elements are in ascending order
public void binarySearch()
{
Scanner sc=new Scanner(System.in);
int []ar={10,20,30,40,50,60,70,80};
System.out.println("Enter the number to be searched");
int key=sc.nextInt();
boolean present= false;
int m,l=0,u=ar.length-1;
while(l<=u)
{
m=(l+u)/2;
if(key==ar[m])
{
present=true;
break;
}
else if(key>ar[m])
{
l=m+1;
}
else
{
u=m-1;
}
}//closing bracket of while loop
if(present==true)
{
System.out.println(key+" is present in the array");
}
else
{
System.out.println(key+" is not present in the array");
}
}
}

EXAMPLE OF SEARCHING A NUMBER PRESENT IN THE SECOND HALF OF THE ARRAY


EXAMPLE OF SEARCHING A NUMBER PRESENT IN THE FIRST HALF OF THE ARRAY
EXAMPLE OF SEARCHING A NUMBER NOT PRESENT IN THE ARRAY
PROGRAM NUMBER 19
Write a program to input and store n numbers in an array ‘list’. Input a number
from the user and use binary search algorithm to check whether the number is
present in the array or not.
import java.util.*;
class BinarySearchDesc
{
private int []list;
private Scanner sc;
//default constructor
public BinarySearchDesc()
{
list=null;
sc=new Scanner(System.in);
}
//function to input and store array elements
public void inputAndStore()
{
int size;
System.out.println(" enter the size of array");
size=sc.nextInt();
list=new int[size];
for(int i=0;i<list.length;i++)
{
System.out.println("Enter the value of list["+i+"]");
list[i]=sc.nextInt();
}
}
//function to perform bubble sort in descending order
public void bubbleSortDesc()
{
int i,j,t;
boolean sorted=true;
for(i=1;i<list.length;i++)
{
for(j=0;j<list.length-i;j++)
{
if(list[j]<list[j+1])
{
t=list[j];
list[j]=list[j+1];
list[j+1]=t;
}
}

}
}
//function to display
public void display()
{
for(int i=0;i<list.length;i++)
{
System.out.print(list[i]+" ");
}
System.out.println();
}
//function to check whether a no is present or not
public void binarySearch(int key)
{
boolean present= false;
int m,l=0,u=list.length-1;
while(l<=u)
{
m=(l+u)/2;
if(key==list[m])
{
present=true;
break;
}
else if(key>list[m])
{
u=m-1;
}
else
{
l=m+1;
}
}
if(present)
{
System.out.println(key+" is present");
}
else
{
System.out.println(key+" is not present");
}
}
//main function
public void main()
{
inputAndStore();
System.out.println("Array elements are");
display();
bubbleSortDesc();
System.out.println("Enter the no. to be searched");
int num=sc.nextInt();
binarySearch(num);
}
}

You might also like