im trying to time stamp this code to plot a graph base on different array size
eg 100 0.454sec
1000 0.467sec
etc
but so far im only getting 0's no matter the array size
and id also want to find out the amount of comparisons made before the search key is found
DESPARATELY need help
thanks

import javax.swing.*;
import java.util.*;
import java.sql.*;
import java.sql.Timestamp;


public class Bsearch
{
public static final int NOT_FOUND = -1;
public static double diffsec;
public static int search(int[]arr, int searchvalue)
{
//time stamp end
long begin = System.currentTimeMillis();
int left = 0;
int right = arr.length;


int val = Bsearch( arr, searchvalue, left, right);
long end = System.currentTimeMillis();
//time stamp end
diffsec = ((double)(end-begin));


return val;
}



private static int Bsearch(int[] arr, int searchValue, int left, int right)
{
if (right < left)
{
return NOT_FOUND;
}
int mid = (left + right) / 2;
if (searchValue > arr[mid])
{
return Bsearch(arr, searchValue, mid + 1, right);
}
else
if (searchValue < arr[mid])
{
return Bsearch(arr, searchValue, left, mid - 1);
}
else
{
return mid;
}
}



public static void main(String[] args)
{
int size = 10;
int[] arr = new int [size*10];
int cnt;
for (cnt = 0; cnt <= arr.length - 1; cnt++)
{
arr[cnt] = ((cnt*12)/2);
}



//***********************************************************************************************************************************



System.out.println("Array of size: "+arr.length);
for(cnt = 0; cnt <arr.length; cnt++)
{


System.out.print(+cnt);
System.out.println("     "+arr[cnt]);
}
System.out.println();
System.out.println("===========================================================");



//***********************************************************************************************************************************



//prompt user to enter search key
String val;
int num;



//**********************************************************************************************************************************
val = JOptionPane.showInputDialog(null, "Enter value u wish to search for in array(0-594): ");
num = Integer.parseInt(val);
System.out.println("Found at Array Index:  "+Bsearch.search(arr, num));
System.out.println("Binary Search:   " +diffsec +" seconds");
}
}


//********************************************************************************************************************************************

Edited 3 Years Ago by happygeek: fixed formatting

Use code tags. Do so by clicking go advanced then clicking the icon at the top. It'll look like this: (And by the way, if you are using a Binary Search Tree, my code may be helpful to you, because at first glance, your code seems to be very wrong. But if you're using a BTree then nevermind). Also, the code I posted below only works for ints, so if you wanted to implement it for other types, you'd have to use the equals method, compareTo, and possibly others.

public static void insert( Integer x )
	{
	   root = insert( x, root );
	}
	
	private static Node insert( Integer x, Node t )
	{
	   if( t == null )
	     return new Node( x, null, null );
	     int compareResult = x.compareTo( t.data );
	   if( compareResult < 0 )
	      t.left = insert( x, t.left );
	   else if( compareResult > 0 )
	      t.right = insert( x, t.right );
	   else
	      ; // Duplicate; do nothing
	     return t;
	}


       private static Node find(Node toFind, Node root){
		if (root == null || root.data == toFind.data)
			return root;
		
		if (toFind.data < root.data)
			return find(toFind, root.left);
		else if (toFind.data > root.data)
			return find(toFind, root.right);
		
		return root;
		
	}

its not a binary search tree.....just a simple binary search
plz help :(

its not a binary search tree.....just a simple binary search
plz help :(

You still need to repost using code tags so it's readable please.

Either:

[code]

// paste code

[/code]

or below (which adds line numbers and syntax highlighting)

[code=JAVA] // paste code

[/code]

its not a binary search tree.....just a simple binary search
plz help :(

I already did help. I only posted that code because I thought it was relevant and I had already written it for personal use. So I'm not going to write the code for you. However, here's how you would do this in pseudocode:

while your value is not found, do the following:
1. Search the root. If the root contains the value you want, return that node and quit.
2. Search the left subtree.
3. Search the right subtree.

Since every "left subtree" has a root, and every "right subtree" has a root, the above algorithm will work. Go make a method, write the code that will follow that algorithm, and then re-run your program. Hint: the method makes recursive calls to itself in order to search the left subtree and the right subtree.

This article has been dead for over six months. Start a new discussion instead.