1
1
namespace Algorithms.Search
2
+
2
3
open System
3
4
4
5
module BinarySearch =
5
- let rec byRecursion ( sortedData : IComparable [], item : int , left : int , right : int ) =
6
+ let rec byRecursion ( sortedData : IComparable [], item : int , left : int , right : int ) =
6
7
7
8
let middle = left + ( right - left) / 2
8
9
9
10
match sortedData.[ middle] with
10
- | s when s.CompareTo( sortedData.[ middle]) > item -> byRecursion( sortedData, item, left, middle - 1 )
11
- | s when s.CompareTo( sortedData.[ middle]) < item -> byRecursion( sortedData, item, left, middle + 1 )
11
+ | s when s.CompareTo( sortedData.[ middle]) > item -> byRecursion ( sortedData, item, left, middle - 1 )
12
+ | s when s.CompareTo( sortedData.[ middle]) < item -> byRecursion ( sortedData, item, left, middle + 1 )
12
13
| _ -> middle
13
14
14
15
/// <summary>
@@ -20,7 +21,7 @@ module BinarySearch =
20
21
/// <param name="sortedData">Sorted array to search in.</param>
21
22
/// <param name="item">Item to search for.</param>
22
23
/// <returns>Index of item that equals to item searched for or -1 if none found.</returns>
23
- let rec findIndex ( sortedData : IComparable [], item : int ) =
24
+ let rec findIndex ( sortedData : IComparable [], item : int ) =
24
25
25
26
let left = 0
26
27
let right = sortedData.Length - 1
@@ -29,6 +30,6 @@ module BinarySearch =
29
30
let currentItem = sortedData.[ middle]
30
31
31
32
match currentItem with
32
- | c when c.CompareTo( sortedData.[ middle]) > item -> findIndex( sortedData, item)
33
- | c when c.CompareTo( sortedData.[ middle]) < item -> findIndex( sortedData, item)
34
- | _ -> item
33
+ | c when c.CompareTo( sortedData.[ middle]) > item -> findIndex ( sortedData, item)
34
+ | c when c.CompareTo( sortedData.[ middle]) < item -> findIndex ( sortedData, item)
35
+ | _ -> item
0 commit comments