File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Can't Find the bug... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can Watch "Can New topic

Can't Find the bug...

Neil Forrest

Joined: Apr 03, 2005
Posts: 13
Can anyone spot the logic bug in my code? It's a binary search program. I'm timing the search times for different inputs. It works up until the input is n=10^6 on the command line. Then it just keeps running. The longest I've let it go is over 12 hours. What I don't get is that for lesser n values, it's really fast. Memory is not the problem...

public class Binary{
//** declarations of variables
int[] list,list_search;
int size,count;
long time1;
long time2;

//** constructor
public Binary(int n, int k){
int i;
list=new int[size];
list_search=new int[count];
time1 = 0;
time2 = 0;

//** generate the list of elements
//sort the list
//search the element

//** sort the list before binary search... done in ascending order
public void sort(){
int i,j,temp;

//** method for binary search
public void binary(int key){
int low=0;
int high=size-1;
int middle;

else if(key<list[middle])
else if(key>list[middle])



public void startsearch(){
int i;
time1 = System.currentTimeMillis();
time2 = System.currentTimeMillis();

System.out.println("Search Time: "+ (time2 - time1) +" ms");


//** main function
public static void main(String args[]){
int n,k;
System.out.println("Required number of arguments are not provided");
n=Integer.parseInt(args[0]);//parsing the size for number of elements in the list
k=Integer.parseInt(args[1]);// parsing the size of number of elements to be searched
Binary ss =new Binary(n,k);//object created to compare the two search

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Well, without even looking at the search routine, I can point out that the sort routine's performance will be proportional to n^2. If it takes 1 millisecond to sort 100 items, it will take over 250 hours (!) to sort 1 million items!

You could replace your custom sort routine with a call to one of the sort() functions in the java.util.Arrays() class -- they perform a n ln n -- which is going to make an enormous difference!

[Jess in Action][AskingGoodQuestions]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
The Java API also contains a binarySearch() method. Of course, if this is a class assignment, there may be some restrictions on what methods you can use from the API. Still, I thought I'd mention it in case you are allowed to use it.


Java API Documentation
The Java Tutorial
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

What I don't get is that for lesser n values, it's really fast.

you have discovered one of the classic issues with this kind of sort. It works fantastic for small sets, but just grinds (almost) to a halt as the input set gets bigger.

As you progress in your studies, you will learn some other sorting methods that will speed things up... most of the time. You can find entire books on nothing but searching and sorting (which are closely related). What makes coding fun and interesting is that you can have two completly different sort algorithms. Depending on the input data (and how clever your sorts are), in one case, one sort will be blindingly fast and the other unbearably slow. The other case will cause the exact opposite. result.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
I agree. Here's the link:
subject: Can't Find the bug...
It's not a secret anymore!