コード例 #1
0
        private void Test()
        {
            System.Diagnostics.Stopwatch watch = System.Diagnostics.Stopwatch.StartNew();

            Random r = new Random();

            int[] array10k = Enumerable.Repeat(0, 10000).Select(i => r.Next()).ToArray();

            SortAlgorithm.Heapsort <int>(array10k, (p1, p2) => p1.CompareTo(p2));

            watch.Stop();
            Debug.WriteLine($"Heapsort, {array10k.Length}, {watch.ElapsedMilliseconds / 1000.0}");
            watch.Restart();

            SortAlgorithm.MergeSort <int>(array10k, (p1, p2) => p1.CompareTo(p2));

            watch.Stop();
            Debug.WriteLine($"MergeSort, {array10k.Length}, {watch.ElapsedMilliseconds / 1000.0}");
            watch.Restart();

            SortAlgorithm.QuickSort <int>(array10k, (p1, p2) => p1.CompareTo(p2));

            watch.Stop();
            Debug.WriteLine($"QuickSort, {array10k.Length}, {watch.ElapsedMilliseconds / 1000.0}");
            watch.Restart();

            SortAlgorithm.BubbleSort <int>(array10k, (p1, p2) => p1.CompareTo(p2));

            watch.Stop();
            Debug.WriteLine($"BubbleSort, {array10k.Length}, {watch.ElapsedMilliseconds / 1000.0}");
            watch.Restart();
        }
コード例 #2
0
        public void BubbleSortTest()
        {
            var expected = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 };
            var actual   = new int[] { 8, 5, 3, 2, 4, 1, 6, 7 };

            Console.WriteLine("Before sort: " + string.Join <int>(",", actual));
            SortAlgorithm.BubbleSort <int>(actual, (a, b) => a < b);
            Console.WriteLine("After sort: " + string.Join <int>(",", actual));
            CollectionAssert.AreEqual(expected, actual);
        }
コード例 #3
0
        public void BubbledSortTest1()
        {
            int        number_count = 100;
            SimpleData data         = new SimpleData();
            var        array        = data.GetShuffleArray(number_count);

            SortAlgorithm algorithm = new SortAlgorithm();

            algorithm.BubbleSort(array);

            for (int i = 0; i < number_count; i++)
            {
                Assert.AreEqual(i, array[i]);
            }
        }
コード例 #4
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)));
            }
        }
コード例 #5
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");
            }
        }
コード例 #6
0
ファイル: frmMain.cs プロジェクト: niujiale/SortComparison
        private void cmdSort_Click(object sender, EventArgs e)
        {
            int speed = 100 - tbSpeed.Value;

            string alg1="";
            string alg2="";

            if(cboAlg1.SelectedItem!=null)
                alg1 = cboAlg1.SelectedItem.ToString();

            if(cboAlg2.SelectedItem!=null)
                alg2 = cboAlg2.SelectedItem.ToString();

            SortAlgorithm sa = new SortAlgorithm(array1, pnlSort1, true, txtOutputFolder.Text, speed, alg1);
            SortAlgorithm sa2 = new SortAlgorithm(array2, pnlSort2, true, txtOutputFolder.Text, speed, alg2);

            ThreadStart ts = delegate()
            {
                try
                {
                    switch (alg1)
                    {
                        case "BiDirectional Bubble Sort":
                            sa.BiDerectionalBubbleSort(array1);
                            break;
                        case "Bubble Sort":
                            sa.BubbleSort(array1);
                            break;
                        case "Bucket Sort":
                            sa.BucketSort(array1);
                            break;
                        case "Comb Sort":
                            sa.CombSort(array1);
                            break;
                        case "Cycle Sort":
                            sa.CycleSort(array1);
                            break;
                        case "Gnome Sort":
                            sa.GnomeSort(array1);
                            break;
                        case "Heap Sort":
                            sa.HeapSort(array1);
                            break;
                        case "Insertion Sort":
                            sa.InsertionSort(array1);
                            break;
                        case "Merge Sort":
                            sa.MergeSort(array1, 0, array1.Count - 1);
                            break;
                        case "Odd-Even Sort":
                            sa.OddEvenSort(array1);
                            break;
                        case "Quick Sort":
                            sa.QuickSort(array1, 0, array1.Count - 1);
                            break;
                        case "Quick Sort with Bubble Sort":
                            sa.QuickSortWithBubbleSort(array1, 0, array1.Count - 1);
                            break;
                        case "Selection Sort":
                            sa.SelectionSort(array1);
                            break;
                        case "Shell Sort":
                            sa.ShellSort(array1);
                            break;
                        case "Pigeon Hole Sort":
                            sa.PigeonHoleSort(array1);
                            break;
                    }
                    if(chkAnimation.Checked)
                        sa.CreateAnimation();
                    MessageBox.Show("Finish");
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.Message);
                }
            };

            ThreadStart ts2 = delegate()
            {
                try
                {
                    switch (alg2)
                    {
                        case "BiDirectional Bubble Sort":
                            sa2.BiDerectionalBubbleSort(array2);
                            break;
                        case "Bubble Sort":
                            sa2.BubbleSort(array2);
                            break;
                        case "Bucket Sort":
                            sa2.BucketSort(array2);
                            break;
                        case "Comb Sort":
                            sa2.CombSort(array2);
                            break;
                        case "Cycle Sort":
                            sa2.CycleSort(array2);
                            break;
                        case "Gnome Sort":
                            sa2.GnomeSort(array2);
                            break;
                        case "Heap Sort":
                            sa2.HeapSort(array2);
                            break;
                        case "Insertion Sort":
                            sa2.InsertionSort(array2);
                            break;
                        case "Merge Sort":
                            sa2.MergeSort(array2, 0, array2.Count - 1);
                            break;
                        case "Odd-Even Sort":
                            sa2.OddEvenSort(array2);
                            break;
                        case "Quick Sort":
                            sa2.QuickSort(array2, 0, array2.Count - 1);
                            break;
                        case "Quick Sort with Bubble Sort":
                            sa2.QuickSortWithBubbleSort(array2, 0, array2.Count - 1);
                            break;
                        case "Selection Sort":
                            sa2.SelectionSort(array2);
                            break;
                        case "Shell Sort":
                            sa2.ShellSort(array2);
                            break;
                        case "Pigeon Hole Sort":
                            sa2.PigeonHoleSort(array2);
                            break;
                    }
                    if (chkAnimation.Checked)
                        sa2.CreateAnimation();
                    MessageBox.Show("Finish");
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.Message);
                }
            };

            if (alg1 != "")
            {
                Thread t = new Thread(ts);
                t.Start();
            }
            if (alg2 != "")
            {
                Thread t2 = new Thread(ts2);
                t2.Start();
            }
        }