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");
}
}
//********************************************************************************************************************************************