Commit 4a9ec40f authored by Tom Almeida's avatar Tom Almeida

added things

parents
// Recursively
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);
}
// Iteratively
template <class T> int binSearch(const T arr[], int len, T what) {
int low = 0;
int high = len - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] > what)
high = mid - 1;
else if (arr[mid] < what)
low = mid + 1;
else
return mid;
}
return -1; // indicate not found
}
// stdlib
#include <algorithm>
bool found = std::binary_search(array, array+len, key, comparitor = null); // Does not return position, just whether it exists
// Recursively
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);
}
// Iteratively
template <class T> int binSearch(const T arr[], int len, T what) {
int low = 0;
int high = len - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] > what)
high = mid - 1;
else if (arr[mid] < what)
low = mid + 1;
else
return mid;
}
return -1; // indicate not found
}
// Recursively
int bsearch (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);
}
}
// Iteratively
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;
}
// Standard library
#include <stdlib.h>
type* p = (type *) bsearch(&key, array, numel, sizeOfSingleElement, comparisonFunction);
int comparisonFunction(const void* pkey, const void* pelem) {};
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>binarysearch.html</title>
</head>
<body>
<h1 id="binary-seach">Binary seach</h1>
<h2 id="complexity-of-onlogn">Complexity of O(<code>nlog(n)</code>)</h2>
<p>Binary search can be implimented either recursively or iteratively.</p>
<h3 id="recursive">Recursive</h3>
<pre><code>// initially called with low = 0, high = N-1
BinarySearch(A[0..N-1], value, low, high) {
// invariants: value &gt; A[i] for all i &lt; low
value &lt; A[i] for all i &gt; high
if (high &lt; low)
return not_found // value would be inserted at index &quot;low&quot;
mid = (low + high) / 2
if (A[mid] &gt; value)
return BinarySearch(A, value, low, mid-1)
else if (A[mid] &lt; value)
return BinarySearch(A, value, mid+1, high)
else
return mid
}</code></pre>
<h3 id="iterative">Iterative</h3>
<pre><code> BinarySearch(A[0..N-1], value) {
low = 0
high = N - 1
while (low &lt;= high) {
// invariants: value &gt; A[i] for all i &lt; low
value &lt; A[i] for all i &gt; high
mid = (low + high) / 2
if (A[mid] &gt; value)
high = mid - 1
else if (A[mid] &lt; value)
low = mid + 1
else
return mid
}
return not_found // value would be inserted at index &quot;low&quot;
}</code></pre>
<h3 id="languages">Languages</h3>
<ul>
<li><a href="../../Rust/Searching/BinarySearch.rs">Rust</a></li>
<li><a href="../../C++/Searching/BinarySearch.cpp">C++</a></li>
<li><a href="../../C/Searching/BinarySearch.c">C</a></li>
</ul>
</body>
</html>
# Binary seach
## Complexity of O(`nlog(n)`)
Binary search can be implimented either recursively or iteratively.
### Recursive
```
// initially called with low = 0, high = N-1
BinarySearch(A[0..N-1], value, low, high) {
// invariants: value > A[i] for all i < low
value < A[i] for all i > high
if (high < low)
return not_found // value would be inserted at index "low"
mid = (low + high) / 2
if (A[mid] > value)
return BinarySearch(A, value, low, mid-1)
else if (A[mid] < value)
return BinarySearch(A, value, mid+1, high)
else
return mid
}
```
### Iterative
```
BinarySearch(A[0..N-1], value) {
low = 0
high = N - 1
while (low <= high) {
// invariants: value > A[i] for all i < low
value < A[i] for all i > high
mid = (low + high) / 2
if (A[mid] > value)
high = mid - 1
else if (A[mid] < value)
low = mid + 1
else
return mid
}
return not_found // value would be inserted at index "low"
}
```
### Languages
* [Rust](../../Rust/Searching/BinarySearch.rs)
* [C++](../../C++/Searching/BinarySearch.cpp)
* [C](../../C/Searching/BinarySearch.c)
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);
}
}
}
This diff is collapsed.
This diff is collapsed.
This org file contains all of the algorithms that I think will be useful for various programming things. There are pseudocode examples as well as implimentations in C, C++ and Rust
* Sorting
* Searching
** [[file:Pseudocode/Searching/binarysearch.html][Binary search]]
* Path finding
* Tree/Graph functions
* Others
* Languages
** C
This section contains all of the algorithms written in C
*** Sorting
*** Searching
**** [[file:C/Searching/BinarySearch.c::int%20comparisonFunction(const%20void*%20pkey,%20const%20void*%20pelem)%20{};][Binary search]]
*** Path finding
*** Tree/Graph functions
*** Others
** C++
This section contains all of the algorithms written in C++
*** Sorting
*** Searching
***** [[file:C++/Searching/BinarySearch.cpp::/%20Recursively][Binary search]]
*** Path finding
*** Tree/Graph functions
*** Others
** Rust
This section contains all of the algorithms written in Rust
*** Sorting
*** Searching
***** [[file:Rust/Searching/BinarySearch.rs::}][Binary search]]
*** Path finding
*** Tree/Graph functions
*** Others
This org file contains all of the algorithms that I think will be useful for various programming things. There are pseudocode examples as well as implimentations in C, C++ and Rust
* Sorting
* Searching
** [[file:Pseudocode/Searching/binarysearch.html][Binary search]]
* Path finding
* Tree/Graph functions
* Others
* Languages
** C
This section contains all of the algorithms written in C
*** Sorting
*** Searching
**** [[file:C/Searching/BinarySearch.c::int%20comparisonFunction(const%20void*%20pkey,%20const%20void*%20pelem)%20{};][Binary search]]
*** Path finding
*** Tree/Graph functions
*** Others
** C++
This section contains all of the algorithms written in C++
*** Sorting
*** Searching
***** [[file:C++/Searching/BinarySearch.cpp::/%20Recursively][Binary search]]
*** Path finding
*** Tree/Graph functions
*** Others
** Rust
This section contains all of the algorithms written in Rust
*** Sorting
*** Searching
***** [[file:Rust/Searching/BinarySearch.rs::}][Binary search]]
*** Path finding
*** Tree/Graph functions
*** Others
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment