Ads

View Answers

June 5, 2009 at 1:00 PM

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

package javaapplication1;

import java.io.BufferedReader;

import java.io.InputStreamReader;

/**

*

* @author Satish

*/

public class BinarySearch {

public static final int NOT_FOUND = -1;

public static int binarySearch( Comparable [ ] a, Comparable x )

{

return binarySearch( a, x, 0, a.length -1 );

}

/**

* Hidden recursive routine.

*/

private static int binarySearch( Comparable [ ] a, Comparable x,

int low, int high )

{

if( low > high )

return NOT_FOUND;

int mid = ( low + high ) / 2;

if( a[ mid ].compareTo( x ) < 0 )

return binarySearch( a, x, mid + 1, high );

else if( a[ mid ].compareTo( x ) > 0 )

return binarySearch( a, x, low, mid - 1 );

else

return mid;

}

// Test program

public static void main( String [ ] args )

{

int SIZE = 8;

Comparable [ ] a = new Integer [ SIZE ];

for( int i = 0; i < SIZE; i++ )

a[ i ] = new Integer( i * 2 );

try{

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

int num ;

num =Integer.parseInt(br.readLine());

System.out.println( "Found " + num + " at " +

binarySearch( a, new Integer( num ) ) );

}

catch(Exception e)

{}

}

}

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

package javaapplication1;

import java.io.BufferedReader;

import java.io.InputStreamReader;

/**

*

* @author Satish

*/

public class BinarySearch {

public static final int NOT_FOUND = -1;

public static int binarySearch( Comparable [ ] a, Comparable x )

{

return binarySearch( a, x, 0, a.length -1 );

}

/**

* Hidden recursive routine.

*/

private static int binarySearch( Comparable [ ] a, Comparable x,

int low, int high )

{

if( low > high )

return NOT_FOUND;

int mid = ( low + high ) / 2;

if( a[ mid ].compareTo( x ) < 0 )

return binarySearch( a, x, mid + 1, high );

else if( a[ mid ].compareTo( x ) > 0 )

return binarySearch( a, x, low, mid - 1 );

else

return mid;

}

// Test program

public static void main( String [ ] args )

{

int SIZE = 8;

Comparable [ ] a = new Integer [ SIZE ];

for( int i = 0; i < SIZE; i++ )

a[ i ] = new Integer( i * 2 );

try{

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

int num ;

num =Integer.parseInt(br.readLine());

System.out.println( "Found " + num + " at " +

binarySearch( a, new Integer( num ) ) );

}

catch(Exception e)

{}

}

}

Related Tutorials/Questions & Answers:

Ads

- Java Tutorials
- Java Code example
- Java Programming
- Java Beginners Examples
- Applet Tutorials
- Awt Tutorials
- Java Certification
- Interview Question
- Java Servlets Tutorial
- Jsp Tutorials
- Java Swing Tutorials
- JDBC Tutorial
- EJB Tutorials
- Java Server Faces (JSF) Tutorial
- WAP Tutorial
- Struts Tutorial
- JAXB Tutorial
- Spring FrameWork Tutorial
- SOA&Web Services Tutorials
- Bioinformatics Tutorials
- MySQL Tutorials
- JAVA DOM Tutorial
- XML Tutorial
- EAI Articles
- Many Programming Tutorials Links
- Tutorials Books
**Java Script Tutorial****Ajax Tutorial****Dojo Tutorials****Programming Books****Trainings****Flex****Ant****RDF**