searching rest
This commit is contained in:
@@ -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 <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)
|
||||
{
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user