Add project files.
This commit is contained in:
3
Sortering.slnx
Normal file
3
Sortering.slnx
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
<Solution>
|
||||||
|
<Project Path="Sortering/Sortering.csproj" />
|
||||||
|
</Solution>
|
||||||
92
Sortering/Program.cs
Normal file
92
Sortering/Program.cs
Normal 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");
|
||||||
|
}
|
||||||
|
}
|
||||||
44
Sortering/Sort.cs
Normal file
44
Sortering/Sort.cs
Normal file
@@ -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<length; n++)
|
||||||
|
{
|
||||||
|
temp = array[n];
|
||||||
|
i = n - 1;
|
||||||
|
|
||||||
|
while (i >= 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<m; n++)
|
||||||
|
{
|
||||||
|
if (array[n] > array[n + 1])
|
||||||
|
{
|
||||||
|
int temp = array[n];
|
||||||
|
array[n] = array[n+1];
|
||||||
|
array[n+1] = temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
10
Sortering/Sortering.csproj
Normal file
10
Sortering/Sortering.csproj
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
Reference in New Issue
Block a user