From 36accc72c6ee765c57f426911fba35fd1aa4a342 Mon Sep 17 00:00:00 2001 From: Tassadar2499 <34176883+Tassadar2499@users.noreply.github.com> Date: Sun, 14 Mar 2021 20:43:28 +0500 Subject: [PATCH] F# and C# addings (#189) * F# - Binary Search * C# - Double selection sort * quick sort - f# --- 01_introduction_to_algorithms/f#/Program.fs | 26 +++++++++++++ .../csharp/double_selection_sort/Program.cs | 39 +++++++++++++++++++ 04_quicksort/f#/QuickSort/Program.fs | 21 ++++++++++ 3 files changed, 86 insertions(+) create mode 100644 01_introduction_to_algorithms/f#/Program.fs create mode 100644 02_selection_sort/csharp/double_selection_sort/Program.cs create mode 100644 04_quicksort/f#/QuickSort/Program.fs diff --git a/01_introduction_to_algorithms/f#/Program.fs b/01_introduction_to_algorithms/f#/Program.fs new file mode 100644 index 0000000..f081473 --- /dev/null +++ b/01_introduction_to_algorithms/f#/Program.fs @@ -0,0 +1,26 @@ +// Learn more about F# at http://fsharp.org + +open System + +[] +let main argv = + let numbers = [|1; 2; 3; 4; 5; 6; 7; 8; 9; 10|] + + let rec binarySearch(arr: int[], number: int, startIndex: int, endIndex: int) : int + = + let averageIndex = (startIndex + endIndex) / 2 + let middleElement = arr.[averageIndex] + + if (middleElement > number) + then binarySearch(arr, number, startIndex, averageIndex) + else if (middleElement < number) + then binarySearch(arr, number, averageIndex, endIndex) + else + averageIndex + + let binarySearch(arr: int[], number: int) + = binarySearch(arr, number, 0, arr.Length) + + let index = binarySearch(numbers, 7) + Console.WriteLine(index); + 0 diff --git a/02_selection_sort/csharp/double_selection_sort/Program.cs b/02_selection_sort/csharp/double_selection_sort/Program.cs new file mode 100644 index 0000000..153bb06 --- /dev/null +++ b/02_selection_sort/csharp/double_selection_sort/Program.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace SelectionSort +{ + class Program + { + static void Main(string[] args) + { + var numbers = new int[] { 4, 5, 1, 3, 10, 9, 6, 8, 7, 2 }; + var sortedArr = SelectionSort(numbers); + + Console.WriteLine(string.Join(',', sortedArr)); + } + + private static int[] SelectionSort(int[] array) + => SelectionSort(new LinkedList(array)).ToArray(); + + private static IEnumerable SelectionSort(LinkedList list) + { + var minList = new LinkedList(); + var maxList = new LinkedList(); + + while (list.Count != 0) + { + var min = list.Min(); + list.Remove(min); + minList.AddLast(min); + + var max = list.Max(); + list.Remove(max); + maxList.AddFirst(max); + } + + return minList.Union(maxList); + } + } +} diff --git a/04_quicksort/f#/QuickSort/Program.fs b/04_quicksort/f#/QuickSort/Program.fs new file mode 100644 index 0000000..e4b8884 --- /dev/null +++ b/04_quicksort/f#/QuickSort/Program.fs @@ -0,0 +1,21 @@ +// Learn more about F# at http://fsharp.org + +open System + +[] +let main argv = + let numbers = [|3; 9; 10; 1; 4; 2; 6; 5; 8; 7|] + + let rec quickSort(arr: int[]) = + if arr.Length < 2 + then arr + else + let pivot = arr.[0] + let arrExcpetPivot = arr |> Seq.skip(1) |> Seq.toArray + let lessElements = quickSort arrExcpetPivot |> Seq.filter(fun n -> n <= pivot) |> Seq.toArray + let greaterElements = quickSort arrExcpetPivot |> Seq.filter(fun n -> n > pivot) |> Seq.toArray + + Seq.append (Seq.append lessElements [|pivot|]) greaterElements |> Seq.toArray + + let sorted = quickSort(numbers) + 0