public void Dijkstra() { Clear(); var path = SearchAlgorithms.DijkstraWithGoal(_graph, _startVertex, _endVertex); StartCoroutine(ShowPath(path)); }
public void TestFindNumber() { int[] nonSortedArray = DummyData <int> .CreateArray(100000, new int[] { 1, 2, 3, 4, 5, 6, 7 }); int[] sortedArray = new int[100000]; for (int i = 0; i < sortedArray.Length; i++) { sortedArray[i] = i + 1; } //Stopwatch watch = Stopwatch.StartNew(); // //bool sorted = SearchAlgorithms.NumericlySorted(sortedArray); // //watch.Stop(); Stopwatch watch = Stopwatch.StartNew(); int index = SearchAlgorithms.FindNumber(sortedArray, 139); watch.Stop(); //Assert.Equal(0, watch.ElapsedTicks); // //Assert.True(sorted); //Assert.Equal(0, watch.ElapsedTicks); Assert.True(sortedArray[index] == 139); }
public void AverageTest() { var expected = 50; var actual = SearchAlgorithms.Average(0, 100); Assert.AreEqual(expected, actual); }
public void DFS() { Clear(); var path = SearchAlgorithms.DepthFirstSearchWithGoal(_graph, _startVertex, _endVertex); StartCoroutine(ShowPath(path)); }
public void BinarySearchTest2() { var array = new int[] { 1, 2, 3, 4, 5, 6, 7 }; var expected = -1; var actual = SearchAlgorithms.BinarySearch <int>(array, 9, (a, b) => a - b); Assert.AreEqual(expected, actual); }
public void RegionGrowTest() { var array = new int[] { 1, 2, 3, 3, 3, 4, 5 }; var expected = new int[] { 3, 3, 3 }; var actual = SearchAlgorithms.RegionGrow <int>(array, 3, (a, b) => a == b); CollectionAssert.AreEqual(expected, actual); }
static void BenchmarkSearch() { SearchAlgorithms.BenchmarkSearch(100000, (int)Math.Pow(2, 10)); SearchAlgorithms.BenchmarkSearch(100000, (int)Math.Pow(2, 11)); SearchAlgorithms.BenchmarkSearch(100000, (int)Math.Pow(2, 12)); SearchAlgorithms.BenchmarkSearch(100000, (int)Math.Pow(2, 13)); SearchAlgorithms.BenchmarkSearch(100000, (int)Math.Pow(2, 14)); SearchAlgorithms.BenchmarkSearch(100000, (int)Math.Pow(2, 15)); }
public void TestForDuplicates() { int[] sortedArray = new int[10] { 1, 2, 3, 4, 5, 6, 6, 6, 9, 10 }; int index = SearchAlgorithms.FindNumber(sortedArray, 6); Assert.Equal(7, index); }
static void Main(string[] args) { double[] nums = { 42, 17, 12, 15, 4, 11, 31, 14 }; int[] A = new int[4]; int[] R = { 4, 5 }; int[] L = { 8, 9 }; Console.WriteLine(string.Join(".", nums)); SimpleSorts <double> .InsertionSort(nums); Console.WriteLine(string.Join(".", nums)); AVLTree <int> tree = new AVLTree <int>(); tree.root = tree.Insert(tree.root, 10); tree.root = tree.Insert(tree.root, 20); tree.root = tree.Insert(tree.root, 30); tree.root = tree.Insert(tree.root, 40); tree.root = tree.Insert(tree.root, 50); tree.root = tree.Insert(tree.root, 25); Console.WriteLine("Preorder traversal" + " of constructed tree is : "); tree.InorderTraversal(tree.root); int searchelement = 17; int[] nums2 = { 42, 17, 12, 15, 4, 11, 31, 14 }; Console.WriteLine(string.Join("", nums)); CountingSorts.BucketSort(nums2); Console.WriteLine(string.Join("", nums)); try { int result = SearchAlgorithms.BinarySeek(searchelement, nums2); Console.WriteLine("Found: " + (nums[result] == searchelement)); } catch (Exception e) { Console.WriteLine("Error"); Console.WriteLine(e.Message); } BTree <int, int> btree = new BTree <int, int>(); btree.Put(19, 19); btree.Put(26, 26); btree.Put(24, 24); btree.Put(14, 14); btree.Put(11, 11); btree.Put(12, 12); btree.Put(14, 14); btree.Put(4, 4); btree.Put(42, 42); Console.WriteLine(btree.ToString()); Console.ReadLine(); }
public void SelectCell(Cell cell) { if (_selectedCell == null) { _selectedCell = cell; _selectedCell.Highlight(true); } else { if (cell == _selectedCell || cell.ID != _selectedCell.ID) { _selectedCell.Highlight(false); _selectedCell = null; Debug.Log("<color=red>id not match or you selected the same cell</color>"); return; } _selectedCell.Active = false; bool found = false; switch (howToSearch) { case SearchType.Circular: { found = SearchAlgorithms.CircularSearch(_map, _selectedCell, cell); if (!showVisuals) { break; } StartCoroutine(SearchAlgorithms.CircularSearchVisual(_map, _selectedCell, line, cell)); break; } case SearchType.DFS: { found = SearchAlgorithms.DFS(_map, _selectedCell, cell); if (!showVisuals) { break; } StartCoroutine(SearchAlgorithms.DFSVisual(_map, _selectedCell, line, cell)); break; } } _selectedCell.Active = true; Debug.Log($"Search returned {found}"); if (found) { ResetCells(cell); } _selectedCell = null; } }
public void TestInsertionSorting() { int[] sortedArray = new int[10] { -1, 2, 3, 6, -5, 6, 4, -6, 9, 6 }; int[] array = DummyData <int> .CreateReverseArray(10000, sortedArray); array = Sorting.InsertionSort(array); bool isSorted = SearchAlgorithms.NumericlySorted(array); Assert.True(isSorted); }
static void ProcessCarplates() { var plates = SwedishCarPlate.GenerateCarPlates((int)Math.Pow(2, 13)); //var plates = SwedishCarPlate.GenerateCarPlates(5); //foreach (var item in plates) //{ // Console.WriteLine(item.CarPlate); //} //SortAlgorithm.BubbleSort<SwedishCarPlate>(plates, SwedishCarPlate.IsLettersOfALessThanB); //var letterSorted = (SwedishCarPlate[]) plates.Clone(); //foreach (var item in letterSorted) //{ // Console.WriteLine(item.CarPlate); //} SortAlgorithm.BubbleSort <SwedishCarPlate>(plates, SwedishCarPlate.IsNumbersOfALessThanB); //var numberSorted = (SwedishCarPlate[])plates.Clone(); //foreach (var item in numberSorted) //{ // Console.WriteLine(item.CarPlate); //} //Console.Write("Search Letters:"); //var input = Console.ReadLine(); //var position = SearchAlgorithms.BinarySearch<SwedishCarPlate>(letterSorted, new SwedishCarPlate { Letters = input }, SwedishCarPlate.CompareLetters); //Console.WriteLine("Position: " + position); Console.Write("Search Numbers:"); var input = Console.ReadLine(); var position = SearchAlgorithms.BinarySearch <SwedishCarPlate>(plates, new SwedishCarPlate { Numbers = input }, SwedishCarPlate.CompareNumbers); if (position > -1) { var similarPlates = SearchAlgorithms.RegionGrow <SwedishCarPlate>(plates, position, (a, b) => SwedishCarPlate.CompareNumbers(a, b) == 0); Console.WriteLine("Position: " + position + " -> " + plates[position].CarPlate); Console.WriteLine(String.Join(", ", similarPlates.Select(a => a.CarPlate))); } }
private static void MeasureSeachTime() { bool worst = false; do { long min = worst ? 10000000 : 10; for (long i = min; i <= min * 10; i += min) { long[] binaryTicks = new long[5]; long[] linearTicks = new long[binaryTicks.Length]; int[] array = new int[i]; for (int n = 0; n < i; n++) { array[n] = n + 1; } for (int runs = 0; runs < binaryTicks.Length; runs++) { Stopwatch watch = Stopwatch.StartNew(); SearchAlgorithms.FindNumber(array, i - 5); watch.Stop(); binaryTicks[runs] = watch.Elapsed.Ticks; watch = Stopwatch.StartNew(); SearchAlgorithms.LinearFindNumber(array, i - 5); watch.Stop(); linearTicks[runs] = watch.Elapsed.Ticks; } Console.WriteLine($"Binary {i}: {binaryTicks.Average()}"); Console.WriteLine($"Linear {i}: {linearTicks.Average()}\n"); } Console.WriteLine(""); worst = !worst; } while(worst); }
private static void Demo() { int[] arr = new int[] { 3, -1, 15, 4, 17, 2, 33, 0 }; var arr2 = new int[] { 1, 2 }; Console.WriteLine("arr = [{0}]", string.Join(", ", arr)); SortingAlgorithms.SelectionSort(arr); Console.WriteLine("sorted = [{0}]", string.Join(", ", arr)); // SortingAlgorithms.SelectionSort(new int[0]); // Test sorting empty array SortingAlgorithms.SelectionSort(new int[1]); // Test sorting single element array Console.WriteLine(SearchAlgorithms.BinarySearch(arr, -1000)); Console.WriteLine(SearchAlgorithms.BinarySearch(arr, 0)); Console.WriteLine(SearchAlgorithms.BinarySearch(arr, 17)); Console.WriteLine(SearchAlgorithms.BinarySearch(arr, 10)); Console.WriteLine(SearchAlgorithms.BinarySearch(arr, 1000)); }
static void CarPlateBenchmark(int repeats) { for (int i = 10; i <= 15; i++) { var plates = SwedishCarPlate.GenerateCarPlates((int)Math.Pow(2, i)); var dict = plates.GroupBy(p => p.Letters, p => p).ToDictionary(g => g.Key, g => g.ToList()); //foreach (var item in dict) //{ // Console.WriteLine(item.Key + ": " + string.Join(",", item.Value.Select(p => p.CarPlate))); //} Stopwatch w = Stopwatch.StartNew(); for (int j = 0; j < repeats; j++) { dict.ContainsKey(SwedishCarPlate.GetRandomLetters()); } w.Stop(); Console.WriteLine($"Time to search in Dictionary {repeats} times: {w.ElapsedMilliseconds} ms"); SortAlgorithm.BubbleSort <SwedishCarPlate>(plates, SwedishCarPlate.IsLettersOfALessThanB); w.Restart(); for (int j = 0; j < repeats; j++) { var plate = new SwedishCarPlate { Letters = SwedishCarPlate.GetRandomLetters() }; //Console.WriteLine("search for: " + plate.CarPlate); var pos = SearchAlgorithms.BinarySearch <SwedishCarPlate>(plates, plate, SwedishCarPlate.CompareLetters); //if (pos >= 0) //{ // var result = SearchAlgorithms.RegionGrow<SwedishCarPlate>(plates, pos, (a, b) => SwedishCarPlate.CompareLetters(a, b) == 0); //} } w.Stop(); Console.WriteLine($"Time to binarySearch {repeats} times: {w.ElapsedMilliseconds} ms"); } }
static void Main(string[] args) { SearchAlgorithms.BinarySearch(arr, 7); }