searching rest
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
using Sortering;
|
using Sortering;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
internal class Program
|
internal class Program
|
||||||
{
|
{
|
||||||
@@ -13,7 +14,9 @@ internal class Program
|
|||||||
{ "insertionsort", SortInsertion },
|
{ "insertionsort", SortInsertion },
|
||||||
{ "bubblesort", SortBubble },
|
{ "bubblesort", SortBubble },
|
||||||
{ "mkarray", MkArray },
|
{ "mkarray", MkArray },
|
||||||
{ "cat", Cat },
|
{ "print", Print },
|
||||||
|
{ "sequentialfind", SequentialFind },
|
||||||
|
{ "binaryfind", BinaryFind },
|
||||||
{ "exit", _ => _running = false }
|
{ "exit", _ => _running = false }
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -144,7 +147,57 @@ internal class Program
|
|||||||
Console.WriteLine($"Sorted array in {sw.ElapsedMilliseconds} ms");
|
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)
|
if (_ints == null)
|
||||||
{
|
{
|
||||||
@@ -166,7 +219,7 @@ internal class Program
|
|||||||
|
|
||||||
_ints = new int[count];
|
_ints = new int[count];
|
||||||
for (int i = 0; i < count; i++)
|
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");
|
Console.WriteLine($"Created array with {count} elements");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user