Compare commits

...

2 Commits

Author SHA1 Message Date
maxwes08
fa609d4306 searching rest 2026-01-26 10:36:39 +01:00
maxwes08
9d19ca6d22 searching 2026-01-26 10:35:50 +01:00
2 changed files with 209 additions and 44 deletions

View File

@@ -1,11 +1,11 @@
using Sortering;
using System.Diagnostics;
using System.Text.RegularExpressions;
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 =
@@ -14,7 +14,10 @@ internal class Program
{ "insertionsort", SortInsertion },
{ "bubblesort", SortBubble },
{ "mkarray", MkArray },
{ "cat", Cat },
{ "print", Print },
{ "sequentialfind", SequentialFind },
{ "binaryfind", BinaryFind },
{ "exit", _ => _running = false }
};
private static void Main(string[] args)
@@ -22,7 +25,7 @@ internal class Program
while (_running)
{
Console.Write("> ");
var input = Console.ReadLine();
var input = ReadLineWithAutocomplete();
if (string.IsNullOrWhiteSpace(input)) continue;
var parts = input.Split(' ', StringSplitOptions.RemoveEmptyEntries);
@@ -33,60 +36,191 @@ internal class Program
action(cmdArgs);
else
{
Console.WriteLine("Unknown command, commands available are:");
foreach (var kvp in Commands)
Console.WriteLine("Unknown command. Available:");
foreach (var c in Commands.Keys)
Console.WriteLine(" - " + c);
}
}
}
// autocompletion
private static string ReadLineWithAutocomplete()
{
Console.WriteLine(kvp.Key);
}
}
var buffer = new List<char>();
int cursor = 0;
}
}
private static void SortInsertion(string[] strings)
while (true)
{
Stopwatch sw = new Stopwatch();
sw.Start();
var key = Console.ReadKey(intercept: true);
if (key.Key == ConsoleKey.Enter)
{
Console.WriteLine();
return new string(buffer.ToArray());
}
if (key.Key == ConsoleKey.Backspace && cursor > 0)
{
buffer.RemoveAt(cursor - 1);
cursor--;
RedrawLine(buffer, cursor);
continue;
}
if (key.Key == ConsoleKey.Tab)
{
Autocomplete(buffer, ref cursor);
continue;
}
if (!char.IsControl(key.KeyChar))
{
buffer.Insert(cursor, key.KeyChar);
cursor++;
RedrawLine(buffer, cursor);
}
}
}
private static void Autocomplete(List<char> buffer, ref int cursor)
{
var text = new string(buffer.ToArray());
var parts = text.Split(' ', StringSplitOptions.RemoveEmptyEntries);
// Only autocomplete the command (first word)
if (parts.Length > 1)
return;
string prefix = parts.Length == 0 ? "" : parts[0];
var matches = Commands.Keys
.Where(c => c.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
.ToList();
if (matches.Count == 1)
{
buffer.Clear();
buffer.AddRange(matches[0]);
cursor = buffer.Count;
RedrawLine(buffer, cursor);
}
}
private static void RedrawLine(List<char> buffer, int cursor)
{
Console.Write("\r> ");
Console.Write(new string(buffer.ToArray()));
Console.Write(" ");
Console.SetCursorPosition(2 + cursor, Console.CursorTop);
}
// commands
private static void SortInsertion(string[] args)
{
if (_ints == null)
{
Console.WriteLine("No array. Use mkarray first.");
return;
}
var sw = Stopwatch.StartNew();
Sort.InsertionSort(_ints);
sw.Stop();
long timeElapsed = sw.ElapsedMilliseconds;
Console.WriteLine("Sorted array in " + timeElapsed.ToString() + "ms");
Console.WriteLine($"Sorted array in {sw.ElapsedMilliseconds} ms");
}
private static void SortBubble(string[] strings)
private static void SortBubble(string[] args)
{
Stopwatch sw = new Stopwatch();
sw.Start();
if (_ints == null)
{
Console.WriteLine("No array. Use mkarray first.");
return;
}
var sw = Stopwatch.StartNew();
Sort.BubbleSort(_ints);
sw.Stop();
long timeElapsed = sw.ElapsedMilliseconds;
Console.WriteLine("Sorted array in " + timeElapsed.ToString() + "ms");
Console.WriteLine($"Sorted array in {sw.ElapsedMilliseconds} ms");
}
private static void Cat(string[] strings)
private static void SequentialFind(string[] args)
{
if (_ints == null)
{
Console.WriteLine("No array. Use mkarray first.");
return;
}
if (args.Length < 1 || !int.TryParse(args[0], out int value))
{
Console.WriteLine("Usage: find <value>");
return;
}
var sw = Stopwatch.StartNew();
int match = Sort.SequentialFind(_ints, value);
sw.Stop();
if (match == -1)
Console.WriteLine($"Found no match in {sw.ElapsedMilliseconds} ms");
else
Console.WriteLine($"Found match at {match} in {sw.ElapsedMilliseconds} ms");
}
private static void BinaryFind(string[] args)
{
if (_ints == null)
{
Console.WriteLine("No array. Use mkarray first.");
return;
}
if (args.Length < 1 || !int.TryParse(args[0], out int value))
{
Console.WriteLine("Usage: find <value>");
return;
}
var sw = Stopwatch.StartNew();
int match = Sort.BinaryFind(_ints, value);
sw.Stop();
if (match == -1)
Console.WriteLine($"Found no match in {sw.ElapsedMilliseconds} ms");
else
Console.WriteLine($"Found match at {match} in {sw.ElapsedMilliseconds} ms");
}
private static void Print(string[] args)
{
if (_ints == null)
{
Console.WriteLine("No array.");
return;
}
for (int i = 0; i < _ints.Length; i++)
{
Console.WriteLine(_ints[i]);
}
}
private static void MkArray(string[] strings)
private static void MkArray(string[] args)
{
if (strings.Length < 1) return;
int count = int.Parse(strings[0]);
if (args.Length < 1 || !int.TryParse(args[0], out int count) || count <= 0)
{
Console.WriteLine("Usage: mkarray <size>");
return;
}
_ints = new int[count];
for (int i = 0; i < count; i++)
_ints[i] = _random.Next(1000 + (int)(count * 0.1));
for (int i=0; i< count; i++)
{
_ints[i] = _random.Next(count);
}
Console.WriteLine("Created array with " + strings[0] + " indices");
Console.WriteLine($"Created array with {count} elements");
}
}

View File

@@ -40,5 +40,36 @@
}
}
}
public static int SequentialFind(int[] haystack, int needle)
{
for (int i = 0; i < haystack.Length; i++)
{
if (haystack[i] == needle)
{
return i;
}
}
return -1;
}
public static int BinaryFind(int[] haystack, int needle)
{
int min = 0;
int max = haystack.Length -1;
int i = -1;
while (min <= max && i == -1)
{
int mid = (min + max) / 2;
if (needle > haystack[mid]) min = mid + 1;
else if (needle < haystack[mid]) max = mid - 1;
else i = mid;
}
return i;
}
}
}