June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -1,24 +1,22 @@
|
|||
template <class T>
|
||||
int binsearch(const T array[], int len, T what)
|
||||
{
|
||||
if (len == 0) return -1;
|
||||
int mid = len / 2;
|
||||
if (array[mid] == what) return mid;
|
||||
if (array[mid] < what) {
|
||||
int result = binsearch(array+mid+1, len-(mid+1), what);
|
||||
if (result == -1) return -1;
|
||||
else return result + mid+1;
|
||||
}
|
||||
if (array[mid] > what)
|
||||
return binsearch(array, mid, what);
|
||||
template <class T> int binsearch(const T array[], int low, int high, T value) {
|
||||
if (high < low) {
|
||||
return -1;
|
||||
}
|
||||
auto mid = (low + high) / 2;
|
||||
if (value < array[mid]) {
|
||||
return binsearch(array, low, mid - 1, value);
|
||||
} else if (value > array[mid]) {
|
||||
return binsearch(array, mid + 1, high, value);
|
||||
}
|
||||
return mid;
|
||||
}
|
||||
|
||||
#include <iostream>
|
||||
int main()
|
||||
{
|
||||
int array[] = {2, 3, 5, 6, 8};
|
||||
int result1 = binsearch(array, sizeof(array)/sizeof(int), 4),
|
||||
result2 = binsearch(array, sizeof(array)/sizeof(int), 8);
|
||||
int result1 = binsearch(array, 0, sizeof(array)/sizeof(int), 4),
|
||||
result2 = binsearch(array, 0, sizeof(array)/sizeof(int), 8);
|
||||
if (result1 == -1) std::cout << "4 not found!" << std::endl;
|
||||
else std::cout << "4 found at " << result1 << std::endl;
|
||||
if (result2 == -1) std::cout << "8 not found!" << std::endl;
|
||||
|
|
|
|||
|
|
@ -1,15 +1,15 @@
|
|||
function binary_search(l, value)
|
||||
function binarysearch(lst::Vector{T}, val::T) where T
|
||||
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
|
||||
high = length(lst)
|
||||
while low ≤ high
|
||||
mid = (low + high) ÷ 2
|
||||
if lst[mid] > val
|
||||
high = mid - 1
|
||||
elseif lst[mid] < val
|
||||
low = mid + 1
|
||||
else
|
||||
return mid
|
||||
return mid
|
||||
end
|
||||
end
|
||||
return -1
|
||||
return 0
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,10 +1,18 @@
|
|||
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
|
||||
function binarysearch(lst::Vector{T}, value::T, low=1, high=length(lst)) where T
|
||||
if isempty(lst) return 0 end
|
||||
if low ≥ high
|
||||
if low > high || lst[low] != value
|
||||
return 0
|
||||
else
|
||||
return low
|
||||
end
|
||||
end
|
||||
mid = (low + high) ÷ 2
|
||||
if lst[mid] > value
|
||||
return binarysearch(lst, value, low, mid-1)
|
||||
elseif lst[mid] < value
|
||||
return binarysearch(lst, value, mid+1, high)
|
||||
else
|
||||
return mid
|
||||
end
|
||||
end
|
||||
|
|
|
|||
38
Task/Binary-search/N-t-roff/binary-search.n
Normal file
38
Task/Binary-search/N-t-roff/binary-search.n
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
.de end
|
||||
..
|
||||
.de array
|
||||
. nr \\$1.c 0 1
|
||||
. de \\$1.push end
|
||||
. nr \\$1..\\\\n+[\\$1.c] \\\\$1
|
||||
. end
|
||||
. de \\$1.pushln end
|
||||
. if \\\\n(.$>0 .\\$1.push \\\\$1
|
||||
. if \\\\n(.$>1 \{ \
|
||||
. shift
|
||||
. \\$1.pushln \\\\$@
|
||||
\}
|
||||
. end
|
||||
..
|
||||
.
|
||||
.de binarysearch
|
||||
. nr min 1
|
||||
. nr max \\n[\\$1.c]
|
||||
. nr guess \\n[min]+\\n[max]/2
|
||||
. while !\\n[\\$1..\\n[guess]]=\\$2 \{ \
|
||||
. ie \\n[\\$1..\\n[guess]]<\\$2 .nr min \\n[guess]+1
|
||||
. el .nr max \\n[guess]-1
|
||||
.
|
||||
. if \\n[min]>\\n[max] \{
|
||||
. nr guess 0
|
||||
. break
|
||||
. \}
|
||||
. nr guess \\n[min]+\\n[max]/2
|
||||
. \}
|
||||
\\n[guess]
|
||||
..
|
||||
.array a
|
||||
.a.pushln 1 4 9 16 25 36 49 64 81 100 121 144
|
||||
.binarysearch a 100
|
||||
.br
|
||||
.ie \n[guess]=0 The item \fBdoesn't exist\fP.
|
||||
.el The item \fBdoes exist\fP.
|
||||
24
Task/Binary-search/PARI-GP/binary-search-2.pari
Normal file
24
Task/Binary-search/PARI-GP/binary-search-2.pari
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
binarysearch(v, x) = {
|
||||
local(
|
||||
minm = 1,
|
||||
maxm = length(v),
|
||||
guess = floor(maxm/2+minm/2)
|
||||
);
|
||||
|
||||
while(v[guess] != x,
|
||||
if(v[guess] < x, minm = guess + 1, maxm = guess - 1);
|
||||
if(minm > maxm,
|
||||
guess = 0;
|
||||
break
|
||||
);
|
||||
guess = floor(maxm/2+minm/2)
|
||||
);
|
||||
|
||||
return(guess);
|
||||
}
|
||||
|
||||
idx = binarysearch([1,4,9,16,25,36,49,64,81,100,121,144], 121);
|
||||
if(idx, \
|
||||
print("Item exists on index ", idx), \
|
||||
print("Item does not exist anywhere.") \
|
||||
)
|
||||
3
Task/Binary-search/Rust/binary-search-1.rust
Normal file
3
Task/Binary-search/Rust/binary-search-1.rust
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
let arr = ["a", "bc", "def", "ghij"];
|
||||
arr.binary_search(&"a"); // Search lexicographically
|
||||
arr.binary_search_by(|e| e.len().cmp(&1)); // Search by length
|
||||
18
Task/Binary-search/Rust/binary-search-2.rust
Normal file
18
Task/Binary-search/Rust/binary-search-2.rust
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
use std::cmp::Ordering::*;
|
||||
fn binary_search<T: Ord>(arr: &[T], elem: &T) -> Option<usize>
|
||||
{
|
||||
let mut size = arr.len();
|
||||
let mut base = 0;
|
||||
|
||||
while size > 0 {
|
||||
size /= 2;
|
||||
let mid = base + size;
|
||||
base = match arr[mid].cmp(elem) {
|
||||
Less => mid,
|
||||
Greater => base,
|
||||
Equal => return Some(mid)
|
||||
};
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
|
@ -1,17 +0,0 @@
|
|||
fn bin_search<T : PartialOrd>(sar : &[T], v : &T) -> Option<usize> {
|
||||
let mut lowi=0;
|
||||
let mut highi=sar.len();
|
||||
loop {
|
||||
if lowi>=highi {
|
||||
return None;
|
||||
}
|
||||
let mi=lowi+(highi-lowi)/2;
|
||||
if sar[mi].lt(v) {
|
||||
lowi=mi+1;
|
||||
} else if sar[mi].gt(v) {
|
||||
highi=mi;
|
||||
} else {
|
||||
return Some(mi);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue