From fa609d43069f9dcc70390b0dd1aa523767e40616 Mon Sep 17 00:00:00 2001 From: maxwes08 Date: Mon, 26 Jan 2026 10:36:39 +0100 Subject: [PATCH] searching rest --- Sortering/Program.cs | 59 +++++++++++++++++++++++++++++++++++++++++--- Sortering/Sort.cs | 31 +++++++++++++++++++++++ 2 files changed, 87 insertions(+), 3 deletions(-) diff --git a/Sortering/Program.cs b/Sortering/Program.cs index 4a85b41..75dbe6f 100644 --- a/Sortering/Program.cs +++ b/Sortering/Program.cs @@ -1,5 +1,6 @@ using Sortering; using System.Diagnostics; +using System.Text.RegularExpressions; internal class Program { @@ -13,7 +14,9 @@ internal class Program { "insertionsort", SortInsertion }, { "bubblesort", SortBubble }, { "mkarray", MkArray }, - { "cat", Cat }, + { "print", Print }, + { "sequentialfind", SequentialFind }, + { "binaryfind", BinaryFind }, { "exit", _ => _running = false } }; @@ -144,7 +147,57 @@ internal class Program Console.WriteLine($"Sorted array in {sw.ElapsedMilliseconds} ms"); } - private static void Cat(string[] args) + 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 "); + 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 "); + 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) { @@ -166,7 +219,7 @@ internal class Program _ints = new int[count]; for (int i = 0; i < count; i++) - _ints[i] = _random.Next(count); + _ints[i] = _random.Next(1000 + (int)(count * 0.1)); Console.WriteLine($"Created array with {count} elements"); } diff --git a/Sortering/Sort.cs b/Sortering/Sort.cs index 791d79b..3ce5201 100644 --- a/Sortering/Sort.cs +++ b/Sortering/Sort.cs @@ -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; + } } }