Time for an 2014 update…
This commit is contained in:
parent
372c577f83
commit
09687c4926
2520 changed files with 34227 additions and 7318 deletions
|
|
@ -1,27 +1,31 @@
|
|||
import std.stdio, std.array;
|
||||
import std.stdio, std.array, std.range, std.traits;
|
||||
|
||||
// Recursive
|
||||
bool binarySearch(T)(in T[] data, in T item) pure nothrow {
|
||||
/// Recursive.
|
||||
bool binarySearch(R, T)(/*in*/ R data, in T x)
|
||||
pure nothrow if (isRandomAccessRange!R &&
|
||||
is(Unqual!T == Unqual!(ElementType!R))) {
|
||||
if (data.empty)
|
||||
return false;
|
||||
immutable i = data.length / 2;
|
||||
immutable mid = data[i];
|
||||
if (mid > item)
|
||||
return binarySearch(data[0 .. i], item);
|
||||
if (mid < item)
|
||||
return binarySearch(data[i+1 .. $], item);
|
||||
if (mid > x)
|
||||
return data[0 .. i].binarySearch(x);
|
||||
if (mid < x)
|
||||
return data[i + 1 .. $].binarySearch(x);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Iterative
|
||||
bool binarySearchIt(T)(const(T)[] data, in T item) pure nothrow {
|
||||
/// Iterative.
|
||||
bool binarySearchIt(R, T)(/*in*/ R data, in T x)
|
||||
pure nothrow if (isRandomAccessRange!R &&
|
||||
is(Unqual!T == Unqual!(ElementType!R))) {
|
||||
while (!data.empty) {
|
||||
immutable i = data.length / 2;
|
||||
immutable mid = data[i];
|
||||
if (mid > item)
|
||||
if (mid > x)
|
||||
data = data[0 .. i];
|
||||
else if (mid < item)
|
||||
data = data[i+1 .. $];
|
||||
else if (mid < x)
|
||||
data = data[i + 1 .. $];
|
||||
else
|
||||
return true;
|
||||
}
|
||||
|
|
@ -29,8 +33,11 @@ bool binarySearchIt(T)(const(T)[] data, in T item) pure nothrow {
|
|||
}
|
||||
|
||||
void main() {
|
||||
immutable items = [2, 4, 6, 8, 9];
|
||||
/*const*/ auto items = [2, 4, 6, 8, 9].assumeSorted;
|
||||
foreach (x; [1, 8, 10, 9, 5, 2])
|
||||
writefln("%2d %5s %5s", x, binarySearch(items, x),
|
||||
binarySearchIt(items, x));
|
||||
writefln("%2d %5s %5s %5s", x,
|
||||
items.binarySearch(x),
|
||||
items.binarySearchIt(x),
|
||||
// Standard Binary Search:
|
||||
!items.equalRange(x).empty);
|
||||
}
|
||||
|
|
|
|||
15
Task/Binary-search/Julia/binary-search-1.julia
Normal file
15
Task/Binary-search/Julia/binary-search-1.julia
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
function binary_search(l, value)
|
||||
low = 1
|
||||
high = length(l)
|
||||
while low <= high
|
||||
mid = int((low+high)/2)
|
||||
if l[mid] > value
|
||||
high = mid-1
|
||||
elseif l[mid] < value
|
||||
low = mid+1
|
||||
else
|
||||
return l[mid]
|
||||
end
|
||||
end
|
||||
return -1
|
||||
end
|
||||
10
Task/Binary-search/Julia/binary-search-2.julia
Normal file
10
Task/Binary-search/Julia/binary-search-2.julia
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
function binary_search(l, value, low = 1, high = -1)
|
||||
high == -1 && (high = length(l))
|
||||
l==[] && (return -1)
|
||||
low >= high &&
|
||||
((low > high || l[low] != value) ? (return -1) : return low)
|
||||
mid = int((low+high)/2)
|
||||
l[mid] > value ? (return binary_search(l, value, low, mid-1)) :
|
||||
l[mid] < value ? (return binary_search(l, value, mid+1, high)) :
|
||||
return mid
|
||||
end
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
sub binary_search {
|
||||
($array_ref, $value, $left, $right) = @_;
|
||||
my ($array_ref, $value, $left, $right) = @_;
|
||||
while ($left <= $right) {
|
||||
$middle = ($right + $left) / 2;
|
||||
my $middle = int(($right + $left) / 2);
|
||||
if ($array_ref->[$middle] == $value) {
|
||||
return 1;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
sub binary_search {
|
||||
($array_ref, $value, $left, $right) = @_;
|
||||
my ($array_ref, $value, $left, $right) = @_;
|
||||
if ($right < $left) {
|
||||
return 0;
|
||||
}
|
||||
$middle = ($right + $left) / 2;
|
||||
my $middle = int(($right + $left) / 2);
|
||||
if ($array_ref->[$middle] == $value) {
|
||||
return 1;
|
||||
}
|
||||
|
|
|
|||
6
Task/Binary-search/Python/binary-search-4.py
Normal file
6
Task/Binary-search/Python/binary-search-4.py
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
from bisect import bisect_left
|
||||
|
||||
def binary_search(a, x, lo=0, hi=None): # can't use a to specify default for hi
|
||||
hi = hi if hi is not None else len(a) # hi defaults to len(a)
|
||||
pos = bisect_left(a,x,lo,hi) # find insertion position
|
||||
return (pos if pos != hi and a[pos] == x else -1) # don't walk off the end
|
||||
12
Task/Binary-search/Python/binary-search-5.py
Normal file
12
Task/Binary-search/Python/binary-search-5.py
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
def binary_search(l, value):
|
||||
low = 0
|
||||
high = len(l)-1
|
||||
while low + 1 < high:
|
||||
mid = (low+high)//2
|
||||
if l[mid] > value:
|
||||
high = mid
|
||||
elif l[mid] < value:
|
||||
low = mid
|
||||
else:
|
||||
return mid
|
||||
return high if abs(l[high] - value) < abs(l[low] - value) else low
|
||||
|
|
@ -1,11 +1,9 @@
|
|||
/*REXX program finds a value in a list using a recursive binary search. */
|
||||
@=' 11 17 29 37 41 59 67 71 79 97 101 107 127 137 149',
|
||||
'163 179 191 197 223 227 239 251 269 277 281 307 311 331 347',
|
||||
'367 379 397 419 431 439 457 461 479 487 499 521 541 557 569',
|
||||
'587 599 613 617 631 641 659 673 701 719 727 739 751 757 769',
|
||||
'787 809 821 827 853 857 877 881 907 929 937 967 991 1009'
|
||||
/*(above) list of strong primes.*/
|
||||
|
||||
@= 11 17 29 37 41 59 67 71 79 97 101 107 127 137 149 163 179 163 179,
|
||||
191 197 223 227 239 251 269 277 281 307 311 331 347 367 379 397 419 431 439,
|
||||
457 461 479 487 499 521 541 557 569 587 599 613 617 631 641 659 673 701 719,
|
||||
727 739 751 757 769 787 809 821 827 853 857 877 881 907 929 937 967 991 1009
|
||||
/* [↑] a list of strong primes.*/
|
||||
parse arg ? . /*get a number the user specified*/
|
||||
if ?=='' then do
|
||||
say; say '*** error! *** no arg specified.'; say
|
||||
|
|
@ -16,19 +14,19 @@ high = words(@)
|
|||
avg=(word(@,1)+word(@,high))/2
|
||||
loc = binarySearch(low,high)
|
||||
|
||||
if loc==-1 then do
|
||||
say ? "wasn't found in the list."
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
end
|
||||
else say ? 'is in the list, its index is:' loc
|
||||
if loc==-1 then do
|
||||
say ? "wasn't found in the list."
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
end
|
||||
else say ? 'is in the list, its index is:' loc
|
||||
say
|
||||
say 'arithmetic mean of the' high "values=" avg
|
||||
say 'arithmetic mean of the' high "values=" avg
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*─────────────────────────────────────BINARYSEARCH subroutine──────────*/
|
||||
/*───────────────────────────────────BINARYSEARCH subroutine────────────*/
|
||||
binarySearch: procedure expose @ ?; parse arg low,high
|
||||
if high<low then return -1
|
||||
if high<low then return -1 /*the item wasn't found in list. */
|
||||
mid=(low+high)%2
|
||||
y=word(@,mid)
|
||||
if ?=y then return mid
|
||||
if y>? then return binarySearch(low,mid-1)
|
||||
return binarySearch(mid+1,high)
|
||||
if ?=y then return mid
|
||||
if y>? then return binarySearch(low, mid-1)
|
||||
return binarySearch(mid+1, high)
|
||||
|
|
|
|||
|
|
@ -1,31 +1,27 @@
|
|||
/*REXX program finds a value in a list using an iterative binary search.*/
|
||||
@=' 3 7 13 19 23 31 43 47 61 73 83 89 103 109 113 131',
|
||||
'139 151 167 181 193 199 229 233 241 271 283 293 313 317 337 349',
|
||||
'353 359 383 389 401 409 421 433 443 449 463 467 491 503 509 523',
|
||||
'547 571 577 601 619 643 647 661 677 683 691 709 743 761 773 797',
|
||||
'811 823 829 839 859 863 883 887 911 919 941 953 971 983 1013'
|
||||
/*(above) list of weak primes. */
|
||||
|
||||
@= 3 7 13 19 23 31 43 47 61 73 83 89 103 109 113 131 139 151 167 181,
|
||||
193 199 229 233 241 271 283 293 313 317 337 349 353 359 383 389 401 409 421 433,
|
||||
443 449 463 467 491 503 509 523 547 571 577 601 619 643 647 661 677 683 691 709,
|
||||
743 761 773 797 811 823 829 839 859 863 883 887 911 919 941 953 971 983 1013
|
||||
/* [↑] a list of weak primes.*/
|
||||
parse arg ? . /*get a number the user specified*/
|
||||
|
||||
if ?=='' then do
|
||||
say; say '*** error! *** no arg specified.'; say
|
||||
exit 13
|
||||
end
|
||||
if ?=='' then do
|
||||
say; say '*** error! *** no arg specified.'; say
|
||||
exit 13
|
||||
end
|
||||
low = 1
|
||||
high = words(@)
|
||||
say 'arithmetic mean of the' high "values=" (word(@,1)+word(@,high))/2
|
||||
say 'arithmetic mean of the' high "values=" (word(@,1)+word(@,high))/2
|
||||
say
|
||||
do while low<=high; mid=(low+high)%2; y=word(@,mid)
|
||||
do while low<=high; mid=(low+high)%2; y=word(@,mid)
|
||||
if ?=y then do
|
||||
say ? 'is in the list, its index is:' mid
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
end
|
||||
|
||||
if ?=y then do
|
||||
say ? 'is in the list, its index is:' mid
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
end
|
||||
|
||||
if y>? then high=mid-1
|
||||
else low=mid+1
|
||||
if y>? then high=mid-1
|
||||
else low=mid+1
|
||||
end /*while*/
|
||||
|
||||
say ? "wasn't found in the list."
|
||||
say ? "wasn't found in the list."
|
||||
/*stick a fork in it, we're done.*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue