From 710036f64b744b9222d4c13be1b68a1c2949935e Mon Sep 17 00:00:00 2001 From: maxwes08 Date: Tue, 20 Jan 2026 13:05:03 +0100 Subject: [PATCH] Add project files. --- Sortering.slnx | 3 ++ Sortering/Program.cs | 92 ++++++++++++++++++++++++++++++++++++++ Sortering/Sort.cs | 44 ++++++++++++++++++ Sortering/Sortering.csproj | 10 +++++ 4 files changed, 149 insertions(+) create mode 100644 Sortering.slnx create mode 100644 Sortering/Program.cs create mode 100644 Sortering/Sort.cs create mode 100644 Sortering/Sortering.csproj diff --git a/Sortering.slnx b/Sortering.slnx new file mode 100644 index 0000000..43d9838 --- /dev/null +++ b/Sortering.slnx @@ -0,0 +1,3 @@ + + + diff --git a/Sortering/Program.cs b/Sortering/Program.cs new file mode 100644 index 0000000..d651102 --- /dev/null +++ b/Sortering/Program.cs @@ -0,0 +1,92 @@ +using Sortering; +using System.Diagnostics; + +internal class Program +{ + private static int[] _ints; + private static bool _running = true; + + private static Random _random = new Random(); + + private static Dictionary> Commands = + new Dictionary>(StringComparer.OrdinalIgnoreCase) + { + { "insertionsort", SortInsertion }, + { "bubblesort", SortBubble }, + { "mkarray", MkArray }, + { "cat", Cat }, + }; + + private static void Main(string[] args) + { + while (_running) + { + Console.Write("> "); + var input = Console.ReadLine(); + if (string.IsNullOrWhiteSpace(input)) continue; + + var parts = input.Split(' ', StringSplitOptions.RemoveEmptyEntries); + var cmd = parts[0]; + var cmdArgs = parts.Length > 1 ? parts[1..] : Array.Empty(); + + if (Commands.TryGetValue(cmd, out var action)) + action(cmdArgs); + else + { + Console.WriteLine("Unknown command, commands available are:"); + foreach (var kvp in Commands) + { + Console.WriteLine(kvp.Key); + } + } + + } + } + + private static void SortInsertion(string[] strings) + { + Stopwatch sw = new Stopwatch(); + sw.Start(); + + Sort.InsertionSort(_ints); + + long timeElapsed = sw.ElapsedMilliseconds; + Console.WriteLine("Sorted array in " + timeElapsed.ToString() + "ms"); + } + + private static void SortBubble(string[] strings) + { + Stopwatch sw = new Stopwatch(); + sw.Start(); + + Sort.BubbleSort(_ints); + + long timeElapsed = sw.ElapsedMilliseconds; + Console.WriteLine("Sorted array in " + timeElapsed.ToString() + "ms"); + } + + private static void Cat(string[] strings) + { + for (int i = 0; i < _ints.Length; i++) + { + Console.WriteLine(_ints[i]); + } + } + + + private static void MkArray(string[] strings) + { + if (strings.Length < 1) return; + + int count = int.Parse(strings[0]); + + _ints = new int[count]; + + for (int i=0; i< count; i++) + { + _ints[i] = _random.Next(count); + } + + Console.WriteLine("Created array with " + strings[0] + " indices"); + } +} \ No newline at end of file diff --git a/Sortering/Sort.cs b/Sortering/Sort.cs new file mode 100644 index 0000000..791d79b --- /dev/null +++ b/Sortering/Sort.cs @@ -0,0 +1,44 @@ +namespace Sortering +{ + public static class Sort + { + public static void InsertionSort(int[] array) + { + int i, n; + int length = array.Length; + int temp; + + if (length < 2) return; + + for (n=1; n= 0 && array[i] > temp) + { + array[i+1] = array[i]; + i--; + } + + array[i+1] = temp; + } + } + + public static void BubbleSort(int[] array) + { + for (int m = array.Length-1; m > 0; m--) + { + for (int n = 0; n array[n + 1]) + { + int temp = array[n]; + array[n] = array[n+1]; + array[n+1] = temp; + } + } + } + } + } +} diff --git a/Sortering/Sortering.csproj b/Sortering/Sortering.csproj new file mode 100644 index 0000000..2150e37 --- /dev/null +++ b/Sortering/Sortering.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + enable + enable + + +