Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b930a07
commit 5d4623e
Showing
2 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |