Skip to content

Instantly share code, notes, and snippets.

View rjperrella's full-sized avatar
💭
I may be slow to respond.

Ron Perrella rjperrella

💭
I may be slow to respond.
View GitHub Profile
@rjperrella
rjperrella / bsearch.pl
Created May 31, 2020 02:35
binary search in perl, using the goto form of tail-recursion elimination
#!/usr/bin/perl
use Test::Simple tests=>4;
sub bsearch {
my ($aref, $item, $lo, $hi) = @_;
return -1 if $lo > $hi;
my $mid = ($hi + $lo) >> 1;
return $mid if ($aref->[$mid] == $item);
if ($item > $aref->[$mid]) {
@_ = ($aref, $item, $mid+1, $hi);
@rjperrella
rjperrella / recursive_bsearch.py
Last active May 31, 2020 02:38
quick implementation of binary search in recursive python
# Not efficient - this is better done as a while loop (unless Python can optimize tail-recursion...)
# return index of item.
# assumes arry is in ascending order.
# return -1 if item is not found in the array.
# interestingly, the bug I had where line 10 had high - low/ 2 still gave correct answers because the array was so small (and
# perhaps also it was the dimensions?
def bsearch(arry, low, high, item):
if low > high:
return -1 # not found

Keybase proof

I hereby claim:

  • I am rjperrella on github.
  • I am rperrella (https://keybase.io/rperrella) on keybase.
  • I have a public key ASAqAZyXvpcf5fXJ3m8YEfnucxPHcAiV_x_6VMT_s-0tVwo

To claim this, I am signing this object: