Ejemplo n.º 1
0
        public void CombSort()
        {
            var algorithm    = new CombSort();
            var sortedVector = algorithm.Sort(DataDefinition.UnsortedVector());

            Assert.Equal(DataDefinition.SortedVector, sortedVector);
        }
Ejemplo n.º 2
0
        public void DecimalNumbers_CombSort()
        {
            var combSort = new CombSort();
            var numbers  = new[] { 5.2, 9.3, -3, 9.2, 10.4, 9.6, -2, 4, 13, 10.7 };
            var expected = new[] { -3, -2, 4, 5.2, 9.2, 9.3, 9.6, 10.4, 10.7, 13 };

            combSort.Sort(numbers);
            Assert.AreEqual(expected, numbers);
        }
Ejemplo n.º 3
0
        public void Characters_CombSort()
        {
            var combSort = new CombSort();
            var word     = "dbca".ToCharArray();
            var expected = "abcd".ToCharArray();

            combSort.Sort(word);
            Assert.AreEqual(expected, word);
        }
Ejemplo n.º 4
0
        public void OnlyPositiveNumbers_CombSort()
        {
            var combSort = new CombSort();
            var numbers  = new double[] { 5, 9, 3, 9, 10, 9, 2, 4, 13, 10 };
            var expected = new double[] { 2, 3, 4, 5, 9, 9, 9, 10, 10, 13 };

            combSort.Sort(numbers);
            Assert.AreEqual(expected, numbers);
        }
Ejemplo n.º 5
0
        public void PositiveAndNegativeNumbers_CombSort()
        {
            var combSort = new CombSort();
            var numbers  = new double[] { 5, 9, -3, 9, 10, 9, -2, 4, 13, 10 };
            var expected = new double[] { -3, -2, 4, 5, 9, 9, 9, 10, 10, 13 };

            combSort.Sort(numbers);
            Assert.AreEqual(expected, numbers);
        }
Ejemplo n.º 6
0
        public void setUp5()
        {
            arr = new int[2000000];

            combSort = new CombSort();
            Random random = new Random();

            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = random.Next();
            }
        }
Ejemplo n.º 7
0
        public void setUp7()
        {
            arr = new int[800000];

            combSort = new CombSort();
            Random random = new Random();

            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = random.Next();
            }
            Array.Sort(arr);
            Array.Reverse(arr);
        }
Ejemplo n.º 8
0
        public void CombSortTest()
        {
            // arrange
            //List<int> items = new List<int> { 57, 7, 471, 359, 311, 1552, 730, 77, 311, 74234, 87, 419, 359 };
            AlgorithmBase <int> comb = new CombSort <int>(items);

            // act
            comb.SortAndGetSpan();

            // assert
            for (int i = 0; i < items.Count; i++)
            {
                Assert.AreEqual(sorted[i], comb.Items[i]);
            }
        }
Ejemplo n.º 9
0
 public SortingBenchmark()
 {
     bubbleSort = new BubbleSort();
     combSort = new CombSort();
     countSort = new CountSort();
     heapSort = new HeapSort();
     insertionSort = new InsertionSort();
     mergeSort = new MergeSort();
     partitionedMergeSort = new PartitionedMergeSort();
     quickSort = new QuickSort();
     quickSortLomuto = new QuickSort_Lomuto();
     radixSort = new RadixSort();
     selectionSort = new SelectionSort();
     shellSort = new ShellSort();
 }
Ejemplo n.º 10
0
        private static void Sort(Algorithm algorithm, int[] array)
        {
            // Sort
            switch (algorithm)
            {
            case Algorithm.QuickSort:
                QuickSort.Sort(array, 0, array.Length - 1);
                break;

            case Algorithm.QuickSortV2:
                QuickSortV2.Sort(array, 0, array.Length - 1);
                break;

            case Algorithm.BubbleSort:
                BubbleSort.Sort(array);
                break;

            case Algorithm.InsertionSort:
                InsertionSort.Sort(array);
                break;

            case Algorithm.MergeSort:
                MergeSort.Sort(array);
                break;

            case Algorithm.SelectionSort:
                SelectionSort.Sort(array);
                break;

            case Algorithm.ShellSort:
                ShellSort.Sort(array);
                break;

            case Algorithm.HeapSort:
                HeapSort.Sort(array);
                break;

            case Algorithm.CombSort:
                CombSort.Sort(array);
                break;

            default:
                throw new NotSupportedException();
            }
        }
Ejemplo n.º 11
0
 public void CombSetUp()
 {
     Sorter = new CombSort();
 }
 //This will show how the num list runs on each algorithm
 //This allows the user to compare them
 public void hitSpeedCompButton()
 {
     algView = !algView;            //Change whether you are looking at the algorithm or speed comparison
     if (algView)                   //If you are now viewing at the algorithm pseudocode
     {
         algText.text = oldAlgText; //Change the text back to the pseudocode
         if (!half)
         {
             algDescription.enabled = true;
         }
         speedCompText.text = "Speed Comparison";
     }
     else//Otherwise display the speed comparison
     {
         oldAlgText = algText.text;
         string storageText = "Bubble Sort:{0} μs\nCocktail Sort: {1} μs\nComb Sort: {2} μs\nHeap Sort: {3}" +
                              " μs\nInsertion Sort: {4} μs\nMerge Sort:{5} μs\nQuick Sort:{6} μs\nShell Sort:{7} μs\nFlash Sort:{8} μs" +
                              "\nBucket Sort:{9} μs\nRadix Sort:{10} μs";
         //Format the text to show each element in microseconds
         algText.text = string.Format(storageText, BubbleSort.BubbleSortTime(new List <int>(copy)),
                                      CocktailSort.CocktailSortTime(new List <int>(copy)), CombSort.CombSortTime(new List <int>(copy)),
                                      HeapSort.HeapSortStartTime(new List <int>(copy)), InsertionSort.InsertionSortTime(new List <int>(copy)),
                                      Mergesort.MergeSortTime(new List <int>(copy), 0, copy.Count - 1), QuickSort.QuickSortStartTime(new List <int>(copy), 0, copy.Count - 1),
                                      ShellSort.ShellSortTime(new List <int>(copy)), FlashSort.FlashSortTime(new List <int>(copy)), BucketSort.BucketSortTime(new List <int>(copy), 34)
                                      , RadixSort.RadixSortTime(new List <int>(copy)));
         algDescription.enabled = false;
         speedCompText.text     = "Algorithm";
     }
 }
Ejemplo n.º 13
0
        private void SortButton_Click(object sender, EventArgs e)
        {
            if (sortedItemsCount > 0)
            {
                (sender as Button).Enabled = false;

                int methods = 1;

                if (SpeedTrackBar.Value == 0)
                {
                    SpeedTrackBar.Enabled = false;
                    methods = allMethods.GetLength(0);
                    VisualPanel.Controls.Clear();
                }

                string methodName = "";
                for (int methodNumber = 1; methodNumber <= methods; methodNumber++)
                {
                    Label label = new Label();
                    AlgorithmBase <SortedItem> algorithm = new AlgorithmBase <SortedItem>();

                    if (SpeedTrackBar.Value > 0)
                    {
                        foreach (RadioButton radioButton in panel3.Controls.OfType <RadioButton>())
                        {
                            if (radioButton.Checked)
                            {
                                if (!Int32.TryParse(radioButton.Name.Substring("radioButton".Length), out methodNumber))
                                {
                                    methodNumber = 1;
                                }
                                break;
                            }
                        }
                    }

                    if (panel3.Controls.Find("radioButton" + methodNumber.ToString(), false).Any())
                    {
                        methodName = panel3.Controls["radioButton" + methodNumber.ToString()].Text;
                    }

                    switch (methodName)
                    {
                    case "Bubble Sort":
                        algorithm = new BubbleSort <SortedItem>();
                        break;

                    case "Cocktail Sort":
                        algorithm = new CocktailSort <SortedItem>();
                        break;

                    case "Insertion Sort":
                        algorithm = new InsertionSort <SortedItem>();
                        break;

                    case "Shell Sort":
                        algorithm = new ShellSort <SortedItem>();
                        break;

                    case "Tree Sort":
                        algorithm = new Algorithm.DataStructures.Tree <SortedItem>();
                        break;

                    case "Heap Sort":
                        algorithm = new Algorithm.DataStructures.Heap <SortedItem>();
                        break;

                    case "Selection Sort":
                        algorithm = new SelectionSort <SortedItem>();
                        break;

                    case "Gnome Sort":
                        algorithm = new GnomeSort <SortedItem>();
                        break;

                    case "Radix Sort":
                        algorithm = new RadixSort <SortedItem>(RadixSortCheckBox.Checked);
                        break;

                    case "Merge Sort":
                        algorithm = new MergeSort <SortedItem>();
                        break;

                    case "Quick Sort":
                        algorithm = new QuickSort <SortedItem>();
                        break;

                    case "Odd-Even Sort":
                        algorithm = new OddEvenSort <SortedItem>();
                        break;

                    case "Comb Sort":
                        algorithm = new CombSort <SortedItem>();
                        break;

                    default:
                        algorithm = new BubbleSort <SortedItem>();
                        break;
                    }
                    //MessageBox.Show("Вы выбрали метод сортировки " + panel3.Controls["radioButton" + methodNumber.ToString()].Text + " : " + methodNumber.ToString());

                    if (SpeedTrackBar.Value > 0)
                    {
                        algorithm.CompareEvent += AlgorithmCompareEvent;
                        algorithm.SwapEvent    += AlgorithmSwapEvent;
                        algorithm.RemoveEvent  += AlgorithmRemoveEvent;
                    }
                    else
                    {
                        int verticalInterval = 15;
                        if (methodNumber > 1 && ((methodNumber - 1) % (VisualPanel.Height / verticalInterval)) == 0)
                        {
                            VisualPanel.Controls.Clear();
                        }
                        label.Name     = "label_" + methodNumber.ToString();
                        label.Text     = "Идет сортировка массива из " + items.Count.ToString() + " элементов по методу " + panel3.Controls["radioButton" + methodNumber.ToString()].Text + " ...";
                        label.AutoSize = true;
                        label.Location = new Point(5, verticalInterval * ((methodNumber - 1) % (VisualPanel.Height / verticalInterval)));
                        VisualPanel.Controls.Add(label);
                        VisualPanel.Refresh();
                    }

                    algorithm.AddRange(items);
                    TimeSpan runTime = algorithm.SortAndGetSpan(!reverseSortCheckBox.Checked);

                    if (SpeedTrackBar.Value == 0)
                    {
                        label.Text = "Сортировка " + items.Count.ToString() + " элементов по методу " + panel3.Controls["radioButton" + methodNumber.ToString()].Text + " завершена.";
                    }
                    else if (methodName == "Heap Sort")
                    {
                        VisualPanel.Controls.Clear();
                        sortedItemsCount = 0;
                        for (int i = 0; i < algorithm.Items.Count; i++)
                        {
                            SortedItem item = new SortedItem(VisualPanel, ++sortedItemsCount, algorithm.Items[i].Value);
                            VisualPanel.Refresh();
                        }
                        VisualPanel.Refresh();
                    }

                    ResultTableLayoutPanel.GetControlFromPosition(methodNumber, 3).Text = runTime.Seconds.ToString() + "." + runTime.Milliseconds.ToString();
                    ResultTableLayoutPanel.GetControlFromPosition(methodNumber, 4).Text = algorithm.ComparisonCount.ToString();
                    ResultTableLayoutPanel.GetControlFromPosition(methodNumber, 5).Text = algorithm.SwapCount.ToString();

                    VisualPanel.Refresh();
                }

                SpeedTrackBar.Enabled = true;

                if (SpeedTrackBar.Value == 0)
                {
                    for (int i = 1; i <= 3; i++)
                    {
                        ResultTableLayoutPanel.Controls["TestsRadioButton_" + i.ToString()].Enabled = true;
                    }
                }
            }
        }
Ejemplo n.º 14
0
 public void setUp1()
 {
     combSort = new CombSort();
     arr      = new int[] { 30, 12, 22, 10, 26, 35, 8, 18, 40, 15 };
     arr2     = new int[] { 8, 10, 12, 15, 18, 22, 26, 30, 35, 40 };
 }
Ejemplo n.º 15
0
 public void setUp6()
 {
     combSort = new CombSort();
     arr      = new int[] { 100, 90, 80, 70, 60, 50, 40, 30, 20, 10, };
     arr2     = new int[] { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
 }
Ejemplo n.º 16
0
 public void Initialize()
 {
     Sorter = new CombSort();
 }
Ejemplo n.º 17
0
        static void Main(string[] args)
        {
            int[] arr = new int[10000];
            Random rnd = new Random();
            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = rnd.Next(10000);
            }
            ISort<int> s = new BubbleSort<int>();
            DateTime dt = DateTime.Now;
            arr = s.Sorting(arr);
            Console.WriteLine("Time for BubbleSort is {0}.", DateTime.Now - dt);

            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = rnd.Next(10000);
            }
            dt = DateTime.Now;
            s = new CocktailSort<int>();
            arr = s.Sorting(arr);
            Console.WriteLine("Time for CocktailSort is {0}.", DateTime.Now - dt);

            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = rnd.Next(10000);
            }
            dt = DateTime.Now;
            s = new EvenOddSort<int>();
            arr = s.Sorting(arr);
            Console.WriteLine("Time for EvenOddSort is {0}.", DateTime.Now - dt);

            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = rnd.Next(10000);
            }
            dt = DateTime.Now;
            s = new CombSort<int>();
            arr = s.Sorting(arr);
            Console.WriteLine("Time for CombSort is {0}.", DateTime.Now - dt);

            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = rnd.Next(10000);
            }
            dt = DateTime.Now;
            s = new GnomeSort<int>();
            arr = s.Sorting(arr);
            Console.WriteLine("Time for GnomeSort is {0}.", DateTime.Now - dt);

            arr = new int[10000];
            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = rnd.Next(10000);
            }
            dt = DateTime.Now;
            s = new InsertionSort<int>();
            arr = s.Sorting(arr);
            Console.WriteLine("Time for InsertionSort is {0}.", DateTime.Now - dt);

            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = rnd.Next(10000);
            }
            dt = DateTime.Now;
            s = new BinaryInsertionSort<int>();
            arr = s.Sorting(arr);
            Console.WriteLine("Time for BinaryInsertionSort is {0}.", DateTime.Now - dt);

            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = rnd.Next(10000);
            }
            dt = DateTime.Now;
            s = new ShellSort<int>();
            arr = s.Sorting(arr);
            Console.WriteLine("Time for ShellSort is {0}.", DateTime.Now - dt);

            arr = new int[1000000];
            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = rnd.Next(1000000);
            }
            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = rnd.Next(10000);
            }
            dt = DateTime.Now;
            s = new HeapSort<int>();
            arr = s.Sorting(arr);
            Console.WriteLine("Time for HeapSort is {0}.", DateTime.Now - dt);
            int ddd = 0;
            for (int i = 0; i < arr.Length - 2; i++)
            {
                //Console.Write(arr[i] + " ");
                if (arr[i] > arr[i + 1]) //Console.WriteLine("Fatal ERROR!!!");
                    ddd++;
            }
            Console.WriteLine("Error count: {0}", ddd);

            dt = DateTime.Now;
            s = new MergeSort<int>();
            arr = s.Sorting(arr);
            Console.WriteLine("Time for MergeSort is {0}.", DateTime.Now - dt);

            //StreamWriter sw = new StreamWriter("C:/Users/suvorovi/1.txt");
            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = rnd.Next(1000000);
                //sw.Write(arr[i] + " ");
            }
            //sw.WriteLine("");
            dt = DateTime.Now;
            s = new QuickSort<int>();
            arr = s.Sorting(arr);
            Console.WriteLine("Time for QuickSort is {0}.", DateTime.Now - dt);
            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = rnd.Next(1000000);
                //sw.Write(arr[i] + " ");
            }
            //sw.WriteLine("");
            dt = DateTime.Now;
            s = new TimSort<int>();
            arr = s.Sorting(arr);
            Console.WriteLine("Time for TimSort is {0}.", DateTime.Now - dt);
            ddd = 0;
            for (int i = 0; i < arr.Length - 2; i++)
            {
                //Console.Write(arr[i] + " ");
                if (arr[i] > arr[i + 1]) //Console.WriteLine("Fatal ERROR!!!");
                    ddd++;
            }
            Console.WriteLine("Error count: {0}", ddd);
            Console.ReadLine();
            //sw.Close();
        }