Education Menu

 

Features Menu

 

Additional Menu

 

Now Online

We have 131 guests online
 

@edulanka

 

Follow US

Twitter You Tube Google Plus
Pinterest Facebook EduLanka Blog

 

Information Zone

eduLanka Sinhala

Welcome to eduLanka.LK, Largest online education web site in Sri Lanka & most popular educational web site among Sri Lankan students. EduLanka, Best nonprofit & educational website is provided large numbers of online lessons, education related course information, teachers information, E books, examination result alerts, goverment job alerts, tuition class information, career guide for school leavers and more resources related to education in Sri Lanka. Edulanka provides education related resources from primary education, O-Level & A-Level education, University & Postgraduate education and vocational training education.

EduLanka IDN Domains http://එඩියුලංකා.ලංකා http://எடியுலங்கா.இலங்கை


jobs Mathematics SLAS Exam Dharma Education Results



 

Inside Online School

Share Us

Home Page Information Technology Java Programing (ජාවා වැඩසටහන්කරණය)

Binary Search In Java - Example
Tuesday, 08 March 2011 20:11

Binary Search - Java Programing Example

Java Array based Binary search algorithm. Binary search Java programing Language sample code here. Terminal IO package need to download is required to completely run this Java program. eduLanka Java based birary search program source code Sri Lanka Java programing Binary search source.


 

==================

JAVA CODE EXAMPLE HERE

==================

 

/*eduLanka Online education web site of Sri Lanka
  -------- w w w . e d u L a n k a . LK ------------
  java programs for University students - who follow courses  -  BIT  -  SLIIT and more
  find more lessons in http://www.edulanka.lk   This e-mail address is being protected from spambots. You need JavaScript enabled to view it
  this programs under GNU licence 
*/
import TerminalIO.*;
class Bsearch
{
public static void main(String arg[])
{
KeyboardReader reader=new KeyboardReader();
int n,data,x,i;
n=reader.readInt("how many element are u enter :");
int[] arr=new int[n];
System.out.println("Enter an integer:");
for(i=0;i<n;i++)
{
data=reader.readInt();
arr[i]=data;
}
bsort(arr);
System.out.println("After sorting");
for(i=0;i<n;i++)
System.out.print(arr[i]+" ");
int temp,j;

x=reader.readInt("\nEnter that You want to find : ");
int y=binarysearch(arr,x);
System.out.println("After searching");
if(y>0)
System.out.println("Find at "+(y+1));
else
System.out.println("Not find");
}

public static void bsort(int array[])
{
int temp;
for(int i=array.length;i>0;i--)
{
for(int j=0;j<i-1;j++)
{
if(array[j]>array[j+1])
{
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
}

}

public static int binarysearch(int list[],int key)
{
int left,right,midpt,size;
size=list.length;
left=0;
right=size-1;
while(left<=right)
{
midpt=(int)((left+right)/2);
if(key==list[midpt])
{
return midpt;
}
else if(key>list[midpt])
left=midpt+1;
else
right=midpt-1;
}
return -1;
}
}

 


Binary Search Complete code Download from Here :

 

Last Updated on Tuesday, 08 March 2011 20:24
 

Add comment

Add comments relevant to this Article. මෙම ලිපියට අදාලව පමණක් අදහස් එකතු කරන්න.


Security code
Refresh

2.jpg

Download Toolbar

 
 

Most Popular Articles

Latest Articles

Copyright © 2005 - 2016 (ඉස්කෝලේ) eduLanka Online Education School of Sri Lanka