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.56 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.55 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.54 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.53 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.52 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.51 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.50 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.49 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.48 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.47 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.46 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.45 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.44 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.43 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.42 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.41 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.40 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.39 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.38 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.37 tracks/fsharp/exercises/binary-search/Example.fs