Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
8
Task/Binary-search/CoffeeScript/binary-search-1.coffee
Normal file
8
Task/Binary-search/CoffeeScript/binary-search-1.coffee
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
binarySearch = (xs, x) ->
|
||||
do recurse = (low = 0, high = xs.length - 1) ->
|
||||
mid = Math.floor (low + high) / 2
|
||||
switch
|
||||
when high < low then NaN
|
||||
when xs[mid] > x then recurse low, mid - 1
|
||||
when xs[mid] < x then recurse mid + 1, high
|
||||
else mid
|
||||
9
Task/Binary-search/CoffeeScript/binary-search-2.coffee
Normal file
9
Task/Binary-search/CoffeeScript/binary-search-2.coffee
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
binarySearch = (xs, x) ->
|
||||
[low, high] = [0, xs.length - 1]
|
||||
while low <= high
|
||||
mid = Math.floor (low + high) / 2
|
||||
switch
|
||||
when xs[mid] > x then high = mid - 1
|
||||
when xs[mid] < x then low = mid + 1
|
||||
else return mid
|
||||
NaN
|
||||
8
Task/Binary-search/CoffeeScript/binary-search-3.coffee
Normal file
8
Task/Binary-search/CoffeeScript/binary-search-3.coffee
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
do (n = 12) ->
|
||||
odds = (it for it in [1..n] by 2)
|
||||
result = (it for it in \
|
||||
(binarySearch odds, it for it in [0..n]) \
|
||||
when not isNaN it)
|
||||
console.assert "#{result}" is "#{[0...odds.length]}"
|
||||
console.log "#{odds} are odd natural numbers"
|
||||
console.log "#{it} is ordinal of #{odds[it]}" for it in result
|
||||
|
|
@ -1,16 +0,0 @@
|
|||
binsearch = (arr, k) ->
|
||||
low = 0
|
||||
high = arr.length - 1
|
||||
while low <= high
|
||||
mid = Math.floor (low + high) / 2
|
||||
return mid if arr[mid] == k
|
||||
if arr[mid] < k
|
||||
low = mid + 1
|
||||
else
|
||||
high = mid - 1
|
||||
null
|
||||
|
||||
arr = [1,3,5,7,9,11]
|
||||
for i in [0..12]
|
||||
pos = binsearch arr, i
|
||||
console.log "found #{i} at pos #{pos}" if pos?
|
||||
|
|
@ -1,9 +1,8 @@
|
|||
import std.stdio, std.array, std.range, std.traits;
|
||||
|
||||
/// Recursive.
|
||||
bool binarySearch(R, T)(/*in*/ R data, in T x)
|
||||
pure nothrow if (isRandomAccessRange!R &&
|
||||
is(Unqual!T == Unqual!(ElementType!R))) {
|
||||
bool binarySearch(R, T)(/*in*/ R data, in T x) pure nothrow @nogc
|
||||
if (isRandomAccessRange!R && is(Unqual!T == Unqual!(ElementType!R))) {
|
||||
if (data.empty)
|
||||
return false;
|
||||
immutable i = data.length / 2;
|
||||
|
|
@ -16,9 +15,8 @@ pure nothrow if (isRandomAccessRange!R &&
|
|||
}
|
||||
|
||||
/// Iterative.
|
||||
bool binarySearchIt(R, T)(/*in*/ R data, in T x)
|
||||
pure nothrow if (isRandomAccessRange!R &&
|
||||
is(Unqual!T == Unqual!(ElementType!R))) {
|
||||
bool binarySearchIt(R, T)(/*in*/ R data, in T x) pure nothrow @nogc
|
||||
if (isRandomAccessRange!R && is(Unqual!T == Unqual!(ElementType!R))) {
|
||||
while (!data.empty) {
|
||||
immutable i = data.length / 2;
|
||||
immutable mid = data[i];
|
||||
|
|
@ -34,7 +32,7 @@ pure nothrow if (isRandomAccessRange!R &&
|
|||
|
||||
void main() {
|
||||
/*const*/ auto items = [2, 4, 6, 8, 9].assumeSorted;
|
||||
foreach (x; [1, 8, 10, 9, 5, 2])
|
||||
foreach (const x; [1, 8, 10, 9, 5, 2])
|
||||
writefln("%2d %5s %5s %5s", x,
|
||||
items.binarySearch(x),
|
||||
items.binarySearchIt(x),
|
||||
|
|
|
|||
120
Task/Binary-search/Eiffel/binary-search.e
Normal file
120
Task/Binary-search/Eiffel/binary-search.e
Normal file
|
|
@ -0,0 +1,120 @@
|
|||
class
|
||||
APPLICATION
|
||||
|
||||
create
|
||||
make
|
||||
|
||||
feature {NONE} -- Initialization
|
||||
|
||||
make
|
||||
local
|
||||
a: ARRAY [INTEGER]
|
||||
keys: ARRAY [INTEGER]
|
||||
do
|
||||
a := <<0, 1, 4, 5, 6, 7, 8, 9,
|
||||
12, 26, 45, 67, 78, 90,
|
||||
98, 123, 211, 234, 456,
|
||||
769, 865, 2345, 3215,
|
||||
14345, 24324>>
|
||||
keys := <<0, 42, 45, 24324, 99999>>
|
||||
across keys as k loop
|
||||
if has_binary (a, k.item) then
|
||||
print ("The array has an element " + k.item.out)
|
||||
else
|
||||
print ("The array has NOT an element " + k.item.out)
|
||||
end
|
||||
print ("%N")
|
||||
end
|
||||
end
|
||||
|
||||
feature -- Search
|
||||
|
||||
has_binary (a: ARRAY [INTEGER]; key: INTEGER): BOOLEAN
|
||||
-- Does `a[a.lower..a.upper]' include an element `key'?
|
||||
require
|
||||
is_sorted (a, a.lower, a.upper)
|
||||
local
|
||||
i: INTEGER
|
||||
do
|
||||
i := where_binary (a, key)
|
||||
if a.lower <= i and i <= a.upper then
|
||||
Result := True
|
||||
else
|
||||
Result := False
|
||||
end
|
||||
end
|
||||
|
||||
where_binary (a: ARRAY [INTEGER]; key: INTEGER): INTEGER
|
||||
-- The index of an element `key' within `a[a.lower..a.upper]' if it exists.
|
||||
-- Otherwise an integer outside `[a.lower..a.upper]'
|
||||
require
|
||||
is_sorted (a, a.lower, a.upper)
|
||||
do
|
||||
Result := where_binary_range (a, key, a.lower, a.upper)
|
||||
end
|
||||
|
||||
where_binary_range (a: ARRAY [INTEGER]; key: INTEGER; low, high: INTEGER): INTEGER
|
||||
-- The index of an element `key' within `a[low..high]' if it exists.
|
||||
-- Otherwise an integer outside `[low..high]'
|
||||
note
|
||||
source: "http://arxiv.org/abs/1211.4470"
|
||||
require
|
||||
is_sorted (a, low, high)
|
||||
local
|
||||
i, j, mid: INTEGER
|
||||
do
|
||||
if low > high then
|
||||
Result := low - 1
|
||||
else
|
||||
from
|
||||
i := low
|
||||
j := high
|
||||
mid := low
|
||||
Result := low - 1
|
||||
invariant
|
||||
low <= i and i <= mid + 1
|
||||
low <= mid and mid <= j and j <= high
|
||||
i <= j
|
||||
has (a, key, i, j) = has (a, key, low, high)
|
||||
until
|
||||
i >= j
|
||||
loop
|
||||
mid := i + (j - i) // 2
|
||||
if a [mid] < key then
|
||||
i := mid + 1
|
||||
else
|
||||
j := mid
|
||||
end
|
||||
variant
|
||||
j - i
|
||||
end
|
||||
if a [i] = key then
|
||||
Result := i
|
||||
end
|
||||
end
|
||||
ensure
|
||||
low <= Result and Result <= high implies a [Result] = key
|
||||
Result < low or Result > high implies not has (a, key, low, high)
|
||||
end
|
||||
|
||||
feature -- Implementation
|
||||
|
||||
is_sorted (a: ARRAY [INTEGER]; low, high: INTEGER): BOOLEAN
|
||||
-- Is `a[low..high]' sorted in nondecreasing order?
|
||||
require
|
||||
a.lower <= low
|
||||
high <= a.upper
|
||||
do
|
||||
Result := across low |..| (high - 1) as i all a [i.item] <= a [i.item + 1] end
|
||||
end
|
||||
|
||||
has (a: ARRAY [INTEGER]; key: INTEGER; low, high: INTEGER): BOOLEAN
|
||||
-- Is there an element `key' in `a[low..high]'?
|
||||
require
|
||||
a.lower <= low
|
||||
high <= a.upper
|
||||
do
|
||||
Result := across low |..| high as i some a [i.item] = key end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
@ -8,8 +8,8 @@ function binary_search(l, value)
|
|||
elseif l[mid] < value
|
||||
low = mid+1
|
||||
else
|
||||
return l[mid]
|
||||
return mid
|
||||
end
|
||||
end
|
||||
return -1
|
||||
return -1
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
function binarySearch (list, value)
|
||||
local function search(low, high)
|
||||
if low > high then return false end
|
||||
local mid = math.floor((low+high)/2)
|
||||
if list[mid] > value then return search(low,mid-1) end
|
||||
if list[mid] < value then return search(mid+1,high) end
|
||||
|
|
|
|||
23
Task/Binary-search/Prolog/binary-search.pro
Normal file
23
Task/Binary-search/Prolog/binary-search.pro
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
bin_search(Elt,List,Result):-
|
||||
length(List,N), bin_search_inner(Elt,List,1,N,Result).
|
||||
|
||||
bin_search_inner(Elt,List,J,J,J):-
|
||||
nth(J,List,Elt).
|
||||
bin_search_inner(Elt,List,Begin,End,Mid):-
|
||||
Begin < End,
|
||||
Mid is (Begin+End) div 2,
|
||||
nth(Mid,List,Elt).
|
||||
bin_search_inner(Elt,List,Begin,End,Result):-
|
||||
Begin < End,
|
||||
Mid is (Begin+End) div 2,
|
||||
nth(Mid,List,MidElt),
|
||||
MidElt < Elt,
|
||||
NewBegin is Mid+1,
|
||||
bin_search_inner(Elt,List,NewBegin,End,Result).
|
||||
bin_search_inner(Elt,List,Begin,End,Result):-
|
||||
Begin < End,
|
||||
Mid is (Begin+End) div 2,
|
||||
nth(Mid,List,MidElt),
|
||||
MidElt > Elt,
|
||||
NewEnd is Mid-1,
|
||||
bin_search_inner(Elt,List,Begin,NewEnd,Result).
|
||||
10
Task/Binary-search/Scala/binary-search-2.scala
Normal file
10
Task/Binary-search/Scala/binary-search-2.scala
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
def binarySearch[A <% Ordered[A]](xs: Seq[A], x: A): Option[Int] = {
|
||||
var (low, high) = (0, xs.size - 1)
|
||||
while (low <= high)
|
||||
(low + high) / 2 match {
|
||||
case mid if xs(mid) > x => high = mid - 1
|
||||
case mid if xs(mid) < x => low = mid + 1
|
||||
case mid => return Some(mid)
|
||||
}
|
||||
None
|
||||
}
|
||||
10
Task/Binary-search/Scala/binary-search-3.scala
Normal file
10
Task/Binary-search/Scala/binary-search-3.scala
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
def testBinarySearch(n: Int) = {
|
||||
val odds = 1 to n by 2
|
||||
val result = (0 to n).flatMap(binarySearch(odds, _))
|
||||
assert(result == (0 until odds.size))
|
||||
println(s"$odds are odd natural numbers")
|
||||
for (it <- result)
|
||||
println(s"$it is ordinal of ${odds(it)}")
|
||||
}
|
||||
|
||||
def main() = testBinarySearch(12)
|
||||
Loading…
Add table
Add a link
Reference in a new issue