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.119 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.118 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.117 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.116 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.115 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.114 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.113 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.111 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.110 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.109 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.108 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.107 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.106 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.105 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.104 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.103 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.102 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.101 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.100 tracks/fsharp/exercises/binary-search/Example.fs
trackler-2.2.1.99 tracks/fsharp/exercises/binary-search/Example.fs