2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -0,0 +1,46 @@
#include <stdio.h>
int bsearch (int *a, int n, int x) {
int i = 0, j = n - 1;
while (i <= j) {
int k = (i + j) / 2;
if (a[k] == x) {
return k;
}
else if (a[k] < x) {
i = k + 1;
}
else {
j = k - 1;
}
}
return -1;
}
int bsearch_r (int *a, int x, int i, int j) {
if (j < i) {
return -1;
}
int k = (i + j) / 2;
if (a[k] == x) {
return k;
}
else if (a[k] < x) {
return bsearch_r(a, x, k + 1, j);
}
else {
return bsearch_r(a, x, i, k - 1);
}
}
int main () {
int a[] = {-31, 0, 1, 2, 2, 4, 65, 83, 99, 782};
int n = sizeof a / sizeof a[0];
int x = 2;
int i = bsearch(a, n, x);
printf("%d is at index %d\n", x, i);
x = 5;
i = bsearch_r(a, x, 0, n - 1);
printf("%d is at index %d\n", x, i);
return 0;
}

View file

@ -3,7 +3,7 @@
(high (1- (length array))))
(do () ((< high low) nil)
(let ((middle (floor (/ (+ low high) 2))))
(let ((middle (floor (+ low high) 2)))
(cond ((> (aref array middle) value)
(setf high (1- middle)))

View file

@ -1,7 +1,7 @@
(defun binary-search (value array &optional (low 0) (high (1- (length array))))
(if (< high low)
nil
(let ((middle (floor (/ (+ low high) 2))))
(let ((middle (floor (+ low high) 2)))
(cond ((> (aref array middle) value)
(binary-search value array low (1- middle)))

View file

@ -0,0 +1,14 @@
bs:{[a;t]
if[0=#a; :_n];
m:_(#a)%2;
if[t>a@m
tmp:_f[(m+1) _ a;t]
:[_n~tmp; :_n; :1+m+tmp]]
if[t<a@m
:_f[m#a;t]]
:m
}
v:8 30 35 45 49 77 79 82 87 97
{bs[v;x]}' v
0 1 2 3 4 5 6 7 8 9

View file

@ -0,0 +1,45 @@
// iterative search:
fun <T : Comparable<T>> Array<T>.binarySearch(target: T): Int {
var hi = size - 1
var lo = 0
while (hi >= lo) {
val guess = lo + (hi - lo) / 2
if (this[guess] > target)
hi = guess - 1
else if (this[guess] < target)
lo = guess + 1
else
return guess
}
return -1
}
// recursive search:
fun <T : Comparable<T>> Array<T>.binarySearch(target: T, lo: Int, hi: Int): Int {
if (hi < lo)
return -1
val guess = (hi + lo) / 2
return if (this[guess] > target)
binarySearch(target, lo, guess - 1)
else if (this[guess] < target)
binarySearch(target, guess + 1, hi)
else
guess
}
fun main(args: Array<String>) {
val a = intArrayOf(1, 3, 4, 5, 6, 7, 8, 9, 10)
var t = 6 // target
var r = a.binarySearch(t)
println(if (r < 0) "$t not found" else "$t found at index $r")
t = 250
r = a.binarySearch(t)
println(if (r < 0) "$t not found" else "$t found at index $r")
t = 6
r = a.binarySearch(t, 0, a.size)
println(if (r < 0) "$t not found" else "$t found at index $r")
t = 250
r = a.binarySearch(t, 0, a.size)
println(if (r < 0) "$t not found" else "$t found at index $r")
}

View file

@ -1,15 +1,16 @@
sub binary_search {
my ($array_ref, $value, $left, $right) = @_;
while ($left <= $right) {
my $middle = int(($right + $left) >> 1);
return 1 if ($array_ref->[$middle] == $value);
if ($value == $array_ref->[$middle]) {
return middle;
} elsif ($value < $array_ref->[$middle]) {
$right = $middle - 1;
} else {
$left = $middle + 1;
my ($array_ref, $value, $left, $right) = @_;
while ($left <= $right) {
my $middle = int(($right + $left) >> 1);
if ($value == $array_ref->[$middle]) {
return $middle;
}
elsif ($value < $array_ref->[$middle]) {
$right = $middle - 1;
}
else {
$left = $middle + 1;
}
}
}
return 0;
return -1;
}

View file

@ -1,13 +1,14 @@
sub binary_search {
my ($array_ref, $value, $left, $right) = @_;
return 0 if ($right < $left);
my $middle = int(($right + $left) >> 1);
return 1 if ($array_ref->[$middle] == $value);
if ($value == $array_ref->[$middle]) {
return middle;
} elsif ($value < $array_ref->[$middle]) {
binary_search($array_ref, $value, $left, $middle - 1);
} else {
binary_search($array_ref, $value, $middle + 1, $right);
}
my ($array_ref, $value, $left, $right) = @_;
return -1 if ($right < $left);
my $middle = int(($right + $left) >> 1);
if ($value == $array_ref->[$middle]) {
return $middle;
}
elsif ($value < $array_ref->[$middle]) {
binary_search($array_ref, $value, $left, $middle - 1);
}
else {
binary_search($array_ref, $value, $middle + 1, $right);
}
}

View file

@ -1,7 +1,7 @@
def binary_search(l, value, low = 0, high = -1):
if not l: return -1
if(high == -1): high = len(l)-1
if low == high:
if low >= high:
if l[low] == value: return low
else: return -1
mid = (low+high)//2

View file

@ -2,7 +2,7 @@ class Array
def binary_search(val, low=0, high=(length - 1))
return nil if high < low
mid = (low + high) >> 1
case var <=> self[mid]
case val <=> self[mid]
when -1
binary_search(val, low, mid - 1)
when 1

View file

@ -0,0 +1,19 @@
10 DATA 2,3,5,6,8,10,11,15,19,20
20 DIM t(10)
30 FOR i=1 TO 10
40 READ t(i)
50 NEXT i
60 LET value=4: GO SUB 100
70 LET value=8: GO SUB 100
80 LET value=20: GO SUB 100
90 STOP
100 REM Binary search
110 LET lo=1: LET hi=10
120 IF lo>hi THEN LET idx=0: GO TO 170
130 LET middle=INT ((hi+lo)/2)
140 IF value<t(middle) THEN LET hi=middle-1: GO TO 120
150 IF value>t(middle) THEN LET lo=middle+1: GO TO 120
160 LET idx=middle
170 PRINT "Value ";value;
180 IF idx=0 THEN PRINT " not found": RETURN
190 PRINT " found at index ";idx: RETURN