Hi,
I have written a binary search program. It runs fine, and I want to see the Bench marking on it. Below is the code. I am sending the an array an the value to the binarysearch function
which is supposed to calculate the search. However, the values are not going to the function. Please advice me
use warnings;
use strict;
use bytes;
use Benchmark;
#**************************Define the variables***********************
+*************
my @binarysearcharray=('1','2','3','4','5');
my $binarysearchkey=4;
sub binarysearch{
my ($array,$key)=@_;
my @array=@$array;
my ($low,$high) = (0,scalar(@$array)); # computes the length of the a
+rray and gives it to $high
while($low <= $high){
my $mid= int(($low+$high)/2);
print "The New mid is : $mid\n";
if($array[$mid] < $key){
$low = $mid + 1;
print " Calculating Lower Bound\n";
}
elsif($array[$mid] == $key)
{
print "Match found at position $array[$mid]\n";
last;
}
else #($array[$high] > $key)
{
$high=$mid-1;
print "Calculating Upper Bound\n";
}
}
}
####################Bench Marking Area###########################
timethese(4,{binarysearch => 'binarysearch(\@binarysearcharray,2)'});
Output
Benchmark: timing 4 iterations of binarysearch...
The New mid is : 0
Calculating Lower Bound
The New mid is : 0
Calculating Lower Bound
The New mid is : 0
Calculating Lower Bound
The New mid is : 0
Calculating Lower Bound
binarysearch: 0 wallclock secs ( 0.00 usr + 0.00 sys = 0.00 CPU)
(warning: too few iterations for a reliable count)