Skip to content

Commit

Permalink
Added binary search.
Browse files Browse the repository at this point in the history
  • Loading branch information
emeryberger committed Aug 6, 2019
1 parent b930a07 commit 5d4623e
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/components/binsearch.js
@@ -0,0 +1,48 @@
"use strict";
exports.__esModule = true;
function binsearch_helper(arr, start, end, v, comparator) {
// console.log("binsearch_helper: " + start + ", " + end + ", comparing against " + v);
if (start >= end) {
return -1; // not found
}
var midpoint = Math.floor((start + end) / 2);
// console.log("midpoint = " + midpoint);
// Find the earliest matching index.
var comparison = comparator(arr[midpoint], v);
// if ((arr[midpoint] === v) && ((midpoint === 0) || (arr[midpoint-1] != v))) {
if (comparison === 0) {
// console.log("A");
// Found it. Is it the earliest one?
if ((midpoint === 0) || (comparator(arr[midpoint - 1], v) != 0)) {
return midpoint;
}
}
if (comparison < 0) {
// console.log("B");
return binsearch_helper(arr, midpoint + 1, end, v, comparator);
}
else {
// console.log("C");
return binsearch_helper(arr, start, midpoint - 1, v, comparator);
}
}
// Find the index of the earliest occurrence of v in arr using binary search.
// Return -1 if not found.
function binsearch(arr, v, comparator) {
if (comparator === void 0) { comparator = undefined; }
if (typeof comparator === "undefined") {
// console.log("undefined");
comparator = (function (a, b) {
console.log("Comparing " + JSON.stringify(a) + " to " + JSON.stringify(b));
if (a === b) {
return 0;
}
if (a < b) {
return -1;
}
return 1;
});
}
return binsearch_helper(arr, 0, arr.length, v, comparator);
}
exports.binsearch = binsearch;
50 changes: 50 additions & 0 deletions src/components/binsearch.ts
@@ -0,0 +1,50 @@
type ComparatorType<T> = (arg1: T, arg2: T) => number;

function binsearch_helper<T>(arr: Array<T>,
start: number,
end: number,
v: T,
comparator: ComparatorType<T>) : number {
// console.log("binsearch_helper: " + start + ", " + end + ", comparing against " + v);
if (start >= end) {
return -1; // not found
}
let midpoint = Math.floor((start + end) / 2);
// console.log("midpoint = " + midpoint);
// Find the earliest matching index.
let comparison = comparator(arr[midpoint], v);
// if ((arr[midpoint] === v) && ((midpoint === 0) || (arr[midpoint-1] != v))) {
if (comparison === 0) {
// console.log("A");
// Found it. Is it the earliest one?
if ((midpoint === 0) || (comparator(arr[midpoint-1], v) != 0)) {
return midpoint;
}
}
if (comparison < 0) {
// console.log("B");
return binsearch_helper(arr, midpoint+1, end, v, comparator);
} else {
// console.log("C");
return binsearch_helper(arr, start, midpoint-1, v, comparator);
}
}

// Find the index of the earliest occurrence of v in arr using binary search.
// Return -1 if not found.
export function binsearch<T>(arr: Array<T>, v: T, comparator : ComparatorType<T> = undefined) {
if (typeof comparator === "undefined") {
// console.log("undefined");
comparator = ((a, b) => {
console.log("Comparing " + JSON.stringify(a) + " to " + JSON.stringify(b));
if (a === b) {
return 0;
}
if (a < b) {
return -1;
}
return 1;
});
}
return binsearch_helper(arr, 0, arr.length, v, comparator);
}

0 comments on commit 5d4623e

Please sign in to comment.