F# and C# addings (#189)
* F# - Binary Search * C# - Double selection sort * quick sort - f#
This commit is contained in:
26
01_introduction_to_algorithms/f#/Program.fs
Normal file
26
01_introduction_to_algorithms/f#/Program.fs
Normal file
@@ -0,0 +1,26 @@
|
||||
// Learn more about F# at http://fsharp.org
|
||||
|
||||
open System
|
||||
|
||||
[<EntryPoint>]
|
||||
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
|
||||
39
02_selection_sort/csharp/double_selection_sort/Program.cs
Normal file
39
02_selection_sort/csharp/double_selection_sort/Program.cs
Normal file
@@ -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<int>(array)).ToArray();
|
||||
|
||||
private static IEnumerable<int> SelectionSort(LinkedList<int> list)
|
||||
{
|
||||
var minList = new LinkedList<int>();
|
||||
var maxList = new LinkedList<int>();
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
21
04_quicksort/f#/QuickSort/Program.fs
Normal file
21
04_quicksort/f#/QuickSort/Program.fs
Normal file
@@ -0,0 +1,21 @@
|
||||
// Learn more about F# at http://fsharp.org
|
||||
|
||||
open System
|
||||
|
||||
[<EntryPoint>]
|
||||
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
|
||||
Reference in New Issue
Block a user