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.1.16 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.15 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.14 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.13 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.12 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.11 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.10 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.9 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.8 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.7 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.6 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.5 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.4 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.3 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.2 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.1 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.0 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.0.6 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.0.5 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.0.4 tracks/fsharp/exercises/binary-search/Example.fs