Latest Tutorials| Questions and Answers|Ask Questions?|Site Map




Have Programming Question? Ask it here!
 
 
 


alak
binary search
1 Answer(s)      4 years and 11 months ago
Posted in : Java Beginners



Advertisement
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)
{}
}
}


Related Tutorials/Questions & Answers:
Binary Search - Java Beginners
Binary Search  how to use Binary think in java give me the Binary Search programm thx
binary search program
binary search program  write a program to searching a string using binary search
Advertisements
binary search program
binary search program  write a program to searching a string using binary search
binary search program
binary search program  write a program to searching a string using binary search
binary search program
binary search program  write a program to searching a string using binary search
binary search program
binary search program  write a program to searching a string using binary search
Binary Search on array
Binary Search on array  What requirement is placed on an array, so that binary search may be used to locate an entry? ? The array elements must form a heap. ? The array must have at least 2 entries. ? The array must
binary search tree
binary search tree  Construct a binary search tree by inserting the following sequence of characters into an empty tree. N O N L I N E A R D A T... for each algorithm. get an item from the user and search the level/levels
binary search tree
binary search tree  Construct a binary search tree by inserting words into an empty tree. "cut your coat according to your cloth" Visit the tree.... get a word from the user and search the level/levels of that word. refer split
JAVA: Recusrion, Binary Search
JAVA: Recusrion, Binary Search  I want to learn about Binary Search... it using a recursive implementation of Binary Search. For the cases when more than one result can be returned, modify Binary Search to return all the elements
Binary Search Tree
Binary Search Tree  Question-1 ) Modify the BinarySearchTree class so that the iterators are fail-fast.Test your class with amain method ? Question-2 ) Modify the BinarySearchTree class so that the BinarySearchTree objects
Binary Search Tree
Binary Search Tree  Question-1 ) Modify the BinarySearchTree class so that the iterators are fail-fast.Test your class with amain method ? Question-2 ) Modify the BinarySearchTree class so that the BinarySearchTree objects
binary search - Java Beginners
binary search  Write a java program to search an array by using recursive binary search.  /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package
Binary Search!!! - Java Beginners
Binary Search!!!  Hi Sir, My question is quite simple. im only... = Integer.parseInt(searchKeyText); position = search(searchKey, arrayList, 0... to continue?"); } } public static int search(int target, int
binary search tree
binary search tree  how can i make binary search tree? i want write a code that make dictionary with binary search tree data structure.please help me...){ IsEmpty(); currentNode = find(root, number); System.out.println("Search
Algorithms: Binary Search
Java Notes: Algorithms: Binary Search Divide in half A fast way to search a sorted array is to use a binary search. The idea is to look at the element... is less than the middle element, do a binary search on the first half
JavaScript Array Binary Search
JavaScript Array Binary Search       The JavaScript Binary Search becomes very useful in case of large Arrays.  The Binary Search algorithm is used to  handle
Binary Search in Java
Binary Search in Java In this section, we are going to search an element from an array using Binary Search. The advantage of a binary search over a linear search is astounding for large numbers. It can be done either recursively
Binary search tree (insertion) urgent!!
Binary search tree (insertion) urgent!!  Create a program to construct a binary search tree consisting of nodes that each stores an integer.... Assume a binary search tree is constructed from the values 14, 35, 2, 3, 39
Which of the following statements are true with respect to binary search
Which of the following statements are true with respect to binary search  Which of the following statements are true with respect to binary search? 1. The array need not be sorted for carrying out binary search 2. Binary search
php array binary search
PHP Binary Search Array is used to search the given value in the array. In php there is no function for the binary search like java or other language. User can implement and use the binary search in php as given below
How to using Binary Search Array Java ?
How to using Binary Search Array Java ?  Hi, I am beginners in Java... functions. The problem is that how to use binary search array in Java. Please give any online reference show that i will implement the binary search array in Java
Java Array Binary Search example
Java Array Binary Search It is a method for searching the array element... the binary search algorithm.  It returns the index of the found element... example demonstrates how to do a binary search on the Java array object
binary search tree from text file
binary search tree from text file  How so I go about constructing a binary search tree from a text file, which has letters and numbers, which must be sorted and printed in ascending order. E.g. Text file contents 3 apples pears
Send me Binary Search - Java Beginners
Send me Binary Search  how to use Binary think in java give me the Binary Search programm thx..  Hi friend, import java.io.*; public class BinarySearchDemo { public static final int NOT_FOUND = -1
Binary Search in Java
Binary Search in Java is used to search an element from an array. Programmers opt for Binary search over linear search when it comes to large numbers. It can... the answer "Not Found". Following is the example of Binary Search in Java: import
How to create binary search tree using an array?
How to create binary search tree using an array?  hello people, pls guide me on the topic above. i have an string array, i want to make a binary search tree based on data inside this array. the array contains names of people
binary
binary  Hi I want to write a program in pascal that ask a user to input a decimal number and then return its binary equivalent in the minimum number of bits required to repesent the number. Thks
search
search  how to develop search box and how to retrive data from database..   Please visit the following link: Search box
SEARCH
SEARCH  how can we do search in jsp...? option for search criteria like name and DOB...   Please visit the following links: http://www.roseindia.net/jsp/user-search.shtml http://www.roseindia.net/servlets/search.shtml
Search
Search   Hi, I have a project in which I am trying to enter "Marathi" (Indian local language) data in JSP using JSTL and trying to search data... and tries to search then It shows no data from database
Binary tree
Binary tree  a. Construct a method to implement a binary tree using an array. b. Implement the binary tree to store numbers in sorted order
Binary tree
Binary tree  hii, What is binary tree?   hello, A binary tree is a tree in which every node has exactly two links i.e left and right link
binary tree
binary tree  can a binary tree be implemented with out comparing...://www.roseindia.net/java/java-get-example/java-binary-tree-code.shtml http://www.roseindia.net/java/java-get-example/java-binary-tree-insert.shtml
binary tree
binary tree  how to count no. of nodes in a binary tree for mlm if it complet tree or incomplet tree in php using mysql db
Algorithms: Recursive Binary Search
Java Notes: Algorithms: Recursive Binary Search Recursive Binary Search... (eg, binary tree traversal). Iterative binary search is more efficient than... 20 21 22 23 24 25 26 27 28 /** Recursive binary search
octal to binary
octal to binary  i want to know how to convert a octal to binary number   Here is a java example that converts octal to binary. import... binary=Integer.toBinaryString(i); System.out.println(binary
program binary
program binary  Hi I want to write a program in pascal that ask a user to input a decimal number and then return its binary equivalent in the minimum number of bits required to repesent the number. In pascal language please
universal binary for ipad iphone
universal binary for ipad iphone  How can i create a universal binary for iPad and iPhone
swing program for decimal to binary
swing program for decimal to binary  swing program for decimal to binary
BINARY TO DECIMAL - Java Beginners
BINARY TO DECIMAL  HELP ME PLSS. PROGRAM THAT CONVERTS BINARY TO DECIMAL  Hi friend, Program to convert Binary To Decimal : import... (System.in); System.out.print ("Enter a binary number "); String str
Java read binary file
Java read binary file  I want Java read binary file example code that is easy to read and learn. Thanks   Hi, Please see the code at Reading binary file into byte array in Java. Thanks   Hi, There is many
Binary to decimal - Java Beginners
Binary to decimal  Need help pls.. i cannot run this program... pls... = Integer.parseInt(JOptionPane.showInputDialog("Input Binary:")); String c = args[0...; String value = JOptionPane.showInputDialog("Input Binary"); int len
Parsing Binary Data in PHP - PHP
Parsing Binary Data in PHP  Is there a better library for working with binary data in PHP
send Binary Data by doGet or doPost?
send Binary Data by doGet or doPost?  which method in doGet or doPost is use to send binary date to server
java binary file io example
java binary file io example  java binary file io example
Search index
Search index  how to write code for advanced search button
binary distributions - Java Server Faces Questions
binary distributions  what are binary distributions
Hibernate Search
Hibernate Search  Where to learn Hibernate Search module? Thanks   Hi, Check the tutorial: Hibernate Search - Complete tutorial on Hibernate Search Thanks
linear search
linear search  write a program to find string using linear search
Advertisements
 

 

 

DMCA.com