View Answers

November 11, 2008 at 4:02 AM

Hi friend,

import java.io.*;

public class BinarySearchDemo {

public static final int NOT_FOUND = -1;

public static <AnyType extends Comparable<? super AnyType>>

int binarySearch(AnyType [] a, AnyType x ){

int low = 0;

int high = a.length - 1;

int mid;

while(low <= high ){

mid = (low + high)/ 2;

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

low = mid + 1;

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

high = mid - 1;

else

return mid;

}

return NOT_FOUND;

}

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

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

System.out.println("Please enter number!");

int SIZE = Integer.parseInt(buff.readLine());

Integer []a = new Integer[SIZE];

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

a[i] = i * 2;

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

System.out.println("Found " + i + " at " + binarySearch(a, i));

}

}

-----------------------------------------

Read for more information.

http://www.roseindia.net/java/

Thanks.

import java.io.*;

public class BinarySearchDemo {

public static final int NOT_FOUND = -1;

public static <AnyType extends Comparable<? super AnyType>>

int binarySearch(AnyType [] a, AnyType x ){

int low = 0;

int high = a.length - 1;

int mid;

while(low <= high ){

mid = (low + high)/ 2;

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

low = mid + 1;

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

high = mid - 1;

else

return mid;

}

return NOT_FOUND;

}

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

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

System.out.println("Please enter number!");

int SIZE = Integer.parseInt(buff.readLine());

Integer []a = new Integer[SIZE];

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

a[i] = i * 2;

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

System.out.println("Found " + i + " at " + binarySearch(a, i));

}

}

-----------------------------------------

Read for more information.

http://www.roseindia.net/java/

Thanks.

Related Tutorials/Questions & Answers:

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 how to use Binary think in java give me the Binary Search programm thx

Advertisements

send Binary Data by doGet or doPost? which method in doGet or doPost is use to send binary date to server

binary search program write a program to searching a string using binary search

binary search program write a program to searching a string using binary search

binary search program write a program to searching a string using binary search

binary search program write a program to searching a string using binary search

binary search program write a program to searching a string using 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 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 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 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

send me example of jmsmq please send me example about jmsmq (java microsoft message queuing ) library

ModuleNotFoundError: No module named 'binary-search' Hi, My... 'binary-search' How to remove the ModuleNotFoundError: No module named 'binary-search' error? Thanks Hi, In your python

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 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 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

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!!! 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

please send me the answer -difference between DriverManager and DataDourse what is Datasourse? What r the advantages? what is the difference between DriverManager and DataDourse

Plz send me answer quckly Respected Sir, myself is pavan shrivastava.i want ask a question that is ( we can't create object of interface then how would possible to create object

JavaScript Array Binary Search The JavaScript Binary Search becomes very useful in case of large Arrays. The Binary Search algorithm is used to handle

error please send me the solution HTTP Status 500 - type Exception report message description The server encountered an internal error () that prevented it from fulfilling this request. exception

error please send me the solution HTTP Status 500 - type Exception report message description The server encountered an internal error () that prevented it from fulfilling this request. exception

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

ModuleNotFoundError: No module named 'binary-file-search' Hi, My... named 'binary-file-search' How to remove the ModuleNotFoundError: No module named 'binary-file-search' error? Thanks Hi, In your

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

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

please send me the banking data base in swings sir, please send me how to create the banking data base program in swings

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

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

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 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

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

send me javascript code please send me code javascript validation code for this html page.pleaseeeeeeeee. a.first:link { color: green;text-decoration:none; } a.first:visited{color:green;text

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".ADS_TO_REPLACE_1 Following is the example of Binary Search

how to send email please give me details with code in jsp,servlet how to send email please give me details with code in jsp,servlet

pls send me the code for login and register pls immediately send me the jsp code for login and registration with validation with java bean in mysql database... Hi friend, This login action code

error is "column cannot be inserted".........give send me the ans DataInputStream dis; try { dis=new DataInputStream(System.in); System.out.println("enter no. of records to be inserted"); int nrec

please send me java script for html validation please send me code for javascript validation .........please send me its urgent a.first:link { color: green;text-decoration:none; } a.first:visited{color:green;text

please send me javascript validation code hallo sir , please send me java script code for this html page.since i want to do validation.i am a new user in java ....please send me its urgent

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

I didn't gat an output for this code, plz send me a correct code Hi, Here is my code: import java.awt.event.*; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import javax.swing.*; import java.sql.*; public

sir, how to convert excel file to csv file using java? please send me sample code. please send me sample code for converting excel file into csv file uisng java

i want to create website,with manu with three level submenu on top side of page please send me code java iam createing wibsite thats using eclipse andtomcat server,please help me to write menu with three level submenu

hex to binary convertion in java HI, im doin a application wch requires to send a binary message to other mobiles, so i need to convert the hex values to binary format.So the binary format could either be a ringtone

hex to binary convertion in java HI, im doin a application wch requires to send a binary message to other mobiles, so i need to convert the hex values to binary format.So the binary format could either be a ringtone

hex to binary convertion in java HI, im doin a application wch requires to send a binary message to other mobiles, so i need to convert the hex values to binary format.So the binary format could either be a ringtone

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 hii, What is binary tree? hello,ADS_TO_REPLACE_1 A binary tree is a tree in which every node has exactly two links i.e left and right link

Ads