Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- open System
- open Microsoft.FSharp.Linq
- open System.Linq
- let GetRandomNumbers count =
- let rnd = System.Random()
- List.init count (fun _ -> (rnd.Next() % 100))
- let rec BinarySearch (arr: Collections.Generic.List<int>, low:int, high:int, value:int) =
- if (high < low) then
- box None;
- else
- Console.WriteLine("Qual o proximo elemento a ser testado?");
- let midGuess = Console.ReadLine();
- let midGuessInt = Convert.ToInt32(midGuess);
- let mid = (low + high) / 2
- if (mid <> midGuessInt) then
- printf "Resposta correta %d" mid
- Console.WriteLine();
- null
- else
- if (arr.[mid] > value) then
- BinarySearch (arr, low, mid-1, value)
- else if (arr.[mid] < value) then
- BinarySearch (arr, mid+1, high, value)
- else
- match box arr.[mid] with
- | null -> box None
- | x -> box(Some(x))
- let PrintIndexes (high: int) =
- for i = 0 to high do
- printf "%d " i
- let PrintList(list: Collections.Generic.List<int>) =
- for i = 0 to list.Count - 1 do
- printf "%d " (list.ElementAt(i))
- [<EntryPoint>]
- let main argv =
- let list = GetRandomNumbers(10);
- let list = list.OrderBy((fun x -> x)).ToList();
- PrintIndexes (list.Count - 1)
- Console.WriteLine();
- PrintList list;
- Console.WriteLine();
- let random = new Random();
- let value = random.Next(0, list.Count - 1);
- printf "Indique quais sao elementos testados para a pesquisa do elemento %d" (list.ElementAt(value))
- Console.WriteLine();
- let result = BinarySearch(list, 0, list.Count - 1, list.ElementAt(value))
- if result <> null then
- Console.WriteLine("Voce acertou");
- else
- Console.WriteLine("Voce errou");
- Console.WriteLine(result)
- Console.WriteLine("Press any key to continue...");
- Console.ReadLine() |> ignore
- 0 // return an integer exit code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement