Sha256: a08e1d7aacc3c42ea73fdd3796363a4ed4d5254aee7bc31ee5182dfe8659bfed

Contents?: true

Size: 590 Bytes

Versions: 274

Compression:

Stored size: 590 Bytes

Contents

module BinarySearch

let rec binarySearchAux index value =
    function
    | [||] -> 
        None
    | input ->   
        let middle = input.Length / 2
        let middleValue = input.[middle]
        
        if value < middleValue then binarySearchAux index value input.[0 .. middle - 1] 
        elif value > middleValue then binarySearchAux (index + middle + 1) value input.[middle + 1 ..]
        else Some (index + middle)

let binarySearch input value =
    if Array.sort input <> input then failwith "The input must be an ordered lists"
    else binarySearchAux 0 value input

Version data entries

274 entries across 274 versions & 1 rubygems

Version Path
trackler-2.2.0.3 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.0.2 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.0.1 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.0.0 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.1.0.55 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.1.0.54 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.1.0.53 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.1.0.52 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.1.0.51 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.1.0.50 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.1.0.49 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.1.0.48 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.1.0.47 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.1.0.46 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.1.0.45 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.1.0.44 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.1.0.43 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.1.0.42 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.1.0.41 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.1.0.40 tracks/fsharp/exercises/binary-search/Example.fs