Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        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 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));
    }
Exemplo n.º 4
0
        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");
            }
        }
Exemplo n.º 5
0
 static void Main(string[] args)
 {
     SearchAlgorithms.BinarySearch(arr, 7);
 }