File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confused about Arrays.binarySearch

 
Harold Beemer
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand how this method works. The output I get is:

Unsorted List
1 1 5 5 5 5 10 15
Search for 1:1
Search for 5:3
Search for 10:6


But shouldn't it be 0, 2, 6 as the first 1 is at index 0 and the first 5 is at index 2???


import java.util.*;

public ArrayOne() {
}

public static void main(String [] args){

Integer [] iol = new Integer[]{ new Integer(5),new Integer(5),new Integer(5),new Integer(5), new Integer(1), new Integer(1), new Integer(15), new Integer(10)};

Arrays.sort(iol);

System.out.println("Unsorted List");
String s = "";

for (int a : iol){
s = s+a + " ";
}
System.out.println(s);

System.out.println("Search for 1:" + Arrays.binarySearch(iol,1));
System.out.println("Search for 5:" + Arrays.binarySearch(iol,5));
System.out.println("Search for 10:" + Arrays.binarySearch(iol,10));

}

}
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should read the API for binarySearch:

"If the array contains multiple elements with the specified value, there is no guarantee which one will be found."
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more thing also there is not method named as BinarySearch which takes Integer Array as an parameter, instead of Integer Array it accepts int array.

Correct the program. and this is the reference of binary Search API

public static int binarySearch(int[] a,
int key)Searches the specified array of ints for the specified value using the binary search algorithm. The array must be sorted (as by the sort method, above) prior to making this call. If it is not sorted, the results are undefined. If the array contains multiple elements with the specified value, there is no guarantee which one will be found.


and the corrected program is:





Be Cool as
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankur,
try with java 1.5
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Satou kurinosuke:
Ankur,
try with java 1.5


I have tried dear, but still there is no method in Arrays Class called as BinarySearch which accepts Integer Array as an Input.

I think that you are asking about for Loop yes that is possible.
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried dear,

No you haven't, and maybe you should read something about autoboxing.

Try to compile with 1.5 and post your compiling error.
[ July 06, 2006: Message edited by: Satou kurinosuke ]
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok Fine,

I realized now. Actually I forgot this concept on my PC I have Jdk 1.4

installed and there it was giving compiling error. That's why I wrote this

statement.



Well Anyways I know autoboxing and autounboxing It's a New Implementations

in jAVA 1.5 in which If we need to pas anywhere int type variable and if we

are passing Integer Type object which will be automatically boxed in Inteer

Object.

Thankx for reminding me this feature.

 
Asha Pathik
Ranch Hand
Posts: 143
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Harold,

This link will help u understand how binary search internally works.

Asha
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic