Ejemplo n.º 1
0
        /// <summary>
        /// Sorts arrays and returns min time
        /// </summary>
        /// <param name="randomArray">Array of integers</param>
        /// <param name="algorithms">Selected algorithms</param>
        /// <returns></returns>
        private static double SortAndGetMinTime(int[] randomArray, int[] algorithms)
        {
            int[] copyArray = new int[randomArray.Length];

            double min = double.MaxValue;

            for (int i = 0; i < algorithms.Length; i++)
            {
                switch (algorithms[i])
                {
                case 1:
                    Array.Copy(randomArray, copyArray, randomArray.Length);
                    InsertionSort.Sort(copyArray);
                    if (InsertionSort.GetTime() < min)
                    {
                        min = InsertionSort.GetTime();
                    }
                    break;

                case 2:
                    Array.Copy(randomArray, copyArray, randomArray.Length);
                    BubbleSort.Sort(copyArray);
                    if (BubbleSort.GetTime() < min)
                    {
                        min = BubbleSort.GetTime();
                    }
                    break;

                case 3:
                    Array.Copy(randomArray, copyArray, randomArray.Length);
                    QuickSort.Sort(copyArray);
                    if (QuickSort.GetTime() < min)
                    {
                        min = QuickSort.GetTime();
                    }
                    break;

                case 4:
                    Array.Copy(randomArray, copyArray, randomArray.Length);
                    HeapSort.Sort(copyArray);
                    if (HeapSort.GetTime() < min)
                    {
                        min = HeapSort.GetTime();
                    }
                    break;

                case 5:
                    Array.Copy(randomArray, copyArray, randomArray.Length);
                    MergeSort.Sort(copyArray);
                    if (MergeSort.GetTime() < min)
                    {
                        min = MergeSort.GetTime();
                    }
                    break;
                }
            }
            return(min);
        }
Ejemplo n.º 2
0
        private void BubbleSortButton_Click(object sender, EventArgs e)
        {
            RefreshItems();

            var bubble = new BubbleSort <SortedItem>(items);

            bubble.CompareEvent += Bubble_CompareEvent;
            bubble.SwapEvent    += Bubble_SwapEvent;
            //bubble.Sort();

            var time = bubble.Sort();

            TimeLbl.Text    = "Время: " + time.Seconds;
            SwapLbl.Text    = "Количество обменов: " + bubble.SwapCount;
            CompareLbl.Text = "Количество сравнений: " + bubble.ComparsionCount;
        }
Ejemplo n.º 3
0
        private void button2_Click(object sender, EventArgs e)
        {
            var sw = new Stopwatch();

            Thread.Sleep(1500);
            sw.Start();
            var rnd = new Random();

            for (int i = 0; i < 20; i++)
            {
                var val  = rnd.Next(0, 100);
                var item = new SortedItem(val);
                items.Add(item);
                SetProperty(label3, item);
            }
            //---------------
            sw.Restart();
            var bubble = new BubbleSort <SortedItem>();

            bubble.Items.AddRange(items);
            bubble.Sort();
            foreach (var i in bubble.Items)
            {
                SetProperty(label4, i);
            }
            sw.Stop();
            label4.Text += $"\t{sw.ElapsedMilliseconds.ToString()} мс";
            //----------------
            sw.Restart();
            var coctail = new KoktailSort <SortedItem>();

            coctail.Items.AddRange(items);
            coctail.Sort();
            foreach (var i in coctail.Items)
            {
                SetProperty(label5, i);
            }
            sw.Stop();
            label5.Text += $"\t{sw.ElapsedMilliseconds.ToString()} мс";
            //-----------------
            sw.Restart();
            var insert = new InsertSort <SortedItem>();

            insert.Items.AddRange(items);
            insert.Sort();
            foreach (var i in insert.Items)
            {
                SetProperty(label6, i);
            }
            sw.Stop();
            label6.Text += $"\t  {sw.ElapsedMilliseconds.ToString()} мс";
            //----------------------
            sw.Restart();
            var shell = new ShellSort <SortedItem>();

            shell.Items.AddRange(items);
            shell.Sort();
            foreach (var i in shell.Items)
            {
                SetProperty(label7, i);
            }
            sw.Stop();
            label7.Text += $"\t{sw.ElapsedMilliseconds.ToString()} мс";
            //----------------------
            items.Clear();
        }