Created
March 28, 2013 00:51
-
-
Save adilakhter/5259589 to your computer and use it in GitHub Desktop.
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
(* | |
* Performs a binary search on the cumulative sums of lengths. | |
* and return a tuple as (minBound, maxBound) | |
* : arr: int[] -> n: int -> (int,int) | |
*) | |
let getBounds a (n:int): int*int = | |
let binarySearch (arr:int[]) (n:int) : (int*int) = | |
let rec binarySearch' (arr:int[]) (n:int) (low:int) (high:int) = | |
if arr.[high] < n then | |
(high, System.Int32.MaxValue) | |
else if high - low <= 1 then | |
if arr.[low] = n then | |
(low, low) | |
else | |
(low,high) | |
else | |
let mid = (high + low)/2 | |
match arr.[mid],n with | |
| (a,b) when a <= b-> binarySearch' arr n mid high | |
| _ -> binarySearch' arr n low mid | |
binarySearch' arr n 0 (arr.Length-1) | |
binarySearch a n | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment