posted 18 years ago
Hi,
My code compiles, and the searches work but the timings won't work, I purposely entered commands that would take extended times, but I still get outputs of 0ms. Obviously I've coded this part wrong but I can't seem to spot where I've gone wrong. I've attached where I think the problem lies. Can anyone see where I've messed up?
//** method for sequential search
public void sequential(int key){
int i;
time1 = System.currentTimeMillis();
for(i=0;i<size;i++){
countSequential++;
if(key==list[i])
break;
}
time2 = System.currentTimeMillis();
}
//** method for binary search
public void binary(int key){
int low=0;
int high=size-1;
int middle;
time3 = System.currentTimeMillis();
while(low<=high){
middle=(low+high)/2;
countBinary++;
if(key==list[middle])
break;
else if(key<list[middle])
high=middle-1;///search low end of the list
else
low=middle+1;///search higher end of the list
}
time4 = System.currentTimeMillis();
}
public void startsearch(){
int i;
for(i=0;i<count;i++){//searching each element once sequentially and then binary in the same list
sequential(list_search[i]);
binary(list_search[i]);
}
System.out.println("The number of comparisions done per element search:");
System.out.println("Sequential: "+countSequential/count);
System.out.println("Search Time: "+(time2 - time1));
System.out.println("Binary: "+countBinary/count);
System.out.println("Search Time: "+ (time4 - time3));
}
Thanks,
Neil