add perl6 code for chapters 1-4 (#49)

This commit is contained in:
ryn1x
2018-01-04 07:46:31 -07:00
committed by Aditya Bhargava
parent dd0100e471
commit 7d48d29139
11 changed files with 194 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
#!/usr/bin/env perl6
use v6.c;
sub binary-search(@arr, $item){
# low and high keep track of which part of the array you'll search in.
my $low = 0;
my $high = @arr.elems - 1;
# While you haven't narrowed it down to one element ...
while $low <= $high {
# ... check the middle element
my $mid = ($low + $high) div 2;
my $guess = @arr[$mid];
# Found the item.
if $guess ~~ $item {
return $mid;
}
# The guess was too high.
if $guess > $item {
$high = $mid - 1;
}
# The guess was too low.
else {
$low = $mid + 1;
}
}
# Item doesn't exist
return Nil;
}
my @arr = 1, 3, 5, 7, 9;
say binary-search(@arr, 3); # => 1
# We use 'Nil' to indicate that the item wasn't found.
say binary-search(@arr, -1); # => Nil