Binary Search

 



C Program:

#include<stdio.h>	
#include<conio.h>	
void main()
{
 int i,n,a[10],key,result;
 
 printf("Enter the no of elements:");
 scanf("%d",&n);
 
 printf("Enter the sorted list elements:\n");
  for(i=0;i <n;i++)
  {
 scanf("%d",&a[i]);
  }
 printf("Enter key element");
 scanf("%d",&key);
 result=binsearch(a,0,n-1,key);
 if(result==-1)
  {
 printf("Key element is not found");
  }
 else
  {
   printf("Key element is found at%d location",result+1);
   //break;
  }
 getch();
}

int binsearch(int a[], int l, int h, int key)
{
 
int mid;
 if(l>h)

  {
  return(-1);
  }
  
 mid=(l+h)/2;
 
 if(key==a[mid])
  {
   return(mid);
  }
  
 if(key>a[mid])
  {
   return(binsearch(a,mid+1,h,key));
  }
 else
  {
   return(binsearch(a,l,mid-1,key));
  }
  
}
C++ Program:
#include<iostreamh>
using namespace std;

int binarysearch(int a[], int lower, int upper, int key){
	int mid=0;
	if(lower>upper){
		return -1;
	}
	
	mid=(lower+upper)/2;
	
	if(a[mid]==key){
		return mid;
	}
	if(key>a[mid]){
		   return(binarysearch(a,mid+1,upper,key));
	}
	else{
		   return(binarysearch(a,lower,upper,key));
	}
}

int main(){
	
	int i=0,n=0,a[10],key=0,result=0;
	
	cout<<"enter the no of elements:";
	cin>>n;
	
	cout<<"enter the stored list elements:\n";
	for(i=0;i<n;i++){
		cin>>a[i];
	}
	
	cout<<"enter the key element:";
	cin>>key;
	
	result=binarysearch(a,0,n-1,key);

	if(result==-1)
  	{
 	cout<<"Key element is not found";
  	}
 	else
  	{
   	cout<<"Key element is found at "<<result+1<<" location";
  	}
	  return 0;	
}

Java Program:
import java.io.*;

public class binary {

    static int binarysearch(int a[], int lower, int upper, int key) {
        int mid = 0;
        if (lower > upper) {
            return -1;
        }

        mid = (lower + upper) / 2;

        if (a[mid] == key) {
            return mid;
        }

        if (key > a[mid]) {
            return (binarysearch(a, mid + 1, upper, key));
        } else {
            return (binarysearch(a, lower, mid - 1, key));
        }
    }

    public static void main(String args[]) throws IOException {

        int i = 0, n = 0, key = 0, result = 0;
        int a[] = new int[10];
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        System.out.println("enter the no of elements:");
        n = Integer.parseInt(br.readLine());

        System.out.println("enter the stored list elements:");
        for (i = 0; i<n; i++) {
            a[i] = Integer.parseInt(br.readLine());
        }

        System.out.println("enter the key element:");
        key = Integer.parseInt(br.readLine());
        ;

        result = binarysearch(a, 0, n - 1, key);

        if (result == -1) {
            System.out.println("Key element is not found");
        } else {
            result++;
            System.out.println("Key element is found at " + result + " location");
        }
        br.close();
    }

}


Output:
Enter the no of elements:5
Enter the sorted list elements:
12
43
65
87
90
Enter key element:43
Key element is found at 2 location

Comments