Sha256: 60dfae428dfa9e305b899225f70cf89ea249122537c99153976ac70a0b49da09

Contents?: true

Size: 582 Bytes

Versions: 122

Compression:

Stored size: 582 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 find input value =
    if Array.sort input <> input then failwith "The input must be an ordered lists"
    else binarySearchAux 0 value input

Version data entries

122 entries across 122 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.179 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.178 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.177 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.176 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.175 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.174 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.173 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.172 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.171 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.170 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.169 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.167 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.166 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.165 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.164 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.163 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.162 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.161 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.160 tracks/fsharp/exercises/binary-search/Example.fs