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.36 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.35 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.34 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.33 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.32 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.31 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.30 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.29 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.28 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.27 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.26 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.25 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.24 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.23 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.22 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.21 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.20 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.19 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.18 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.17 tracks/fsharp/exercises/binary-search/Example.fs