Binary search and selection sort in Perl

* Create 01_binary_search.pl

* Typo fixed in 01_binary_search.pl

* Create 01_selection_sort.pl
This commit is contained in:
biosta
2018-04-20 18:21:28 +03:00
committed by Aditya Bhargava
parent ea74644c95
commit f24b9a6e35
2 changed files with 75 additions and 0 deletions

View File

@@ -0,0 +1,43 @@
sub binary_search {
my ( $item, @list ) = @_;
# The borders inside where we are searching in:
my $low = 0;
my $high = $#list;
# Do that until found or until just one element remains:
while ( $low <= $high ) {
# Detect the middle of the array:
my $mid = int( ( $low + $high ) / 2 );
my $guess = $list[$mid];
# We found it out:
if ( $guess == $item ) {
return $mid;
}
# The guess is too high, decrease it:
elsif ( $guess > $item ) {
$high = --$mid;
}
# The guess is too low, increase it:
else {
$low = ++$mid;
}
}
# Nothing at all found:
return 'None';
}
my @my_list = qw /1 3 5 7 9/;
# Should display "1":
print binary_search( 3, @my_list ), "\n";
# Should display "None":
print binary_search( -1, @my_list ), "\n";