Add project files.

This commit is contained in:
maxwes08
2026-01-20 13:05:03 +01:00
parent f57aa8acad
commit 710036f64b
4 changed files with 149 additions and 0 deletions

92
Sortering/Program.cs Normal file
View File

@@ -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<string, Action<string[]>> Commands =
new Dictionary<string, Action<string[]>>(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<string>();
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");
}
}