Exemplo n.º 1
0
        private void InsertSortButton_Click(object sender, EventArgs e)
        {
            CleaningLabels();
            var insert = new InsertSort <SortedItem>(items);

            BtnClick(insert);
        }
Exemplo n.º 2
0
        private static void Sort(string[] a, int lo, int hi, int d, int R, string[] aux)
        {
            if (hi <= lo + M)
            {
                InsertSort.Sort(a, lo, hi, d); return;
            }

            int[] count = new int[R + 2];
            // 频率计数
            for (int i = lo; i <= hi; i++)
            {
                count[CharAt(a[i], d)]++;
            }

            // 计算每个字符的种类的起始索引
            for (int r = 0; r < R + 1; r++)
            {
                count[r + 1] += count[r];
            }

            for (int i = 0; i <= hi; i++)
            {
                aux[count[CharAt(a[i], d) + 1]++] = a[i];
            }

            for (int i = lo; i <= hi; i++)
            {
                a[i] = aux[i - lo];
            }

            for (int r = 0; r < R; r++)
            {
                Sort(a, lo + count[r], lo + count[r + 1] - 1, d + 1, R, aux);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Основное действие
        /// </summary>
        /// <param name="Fill">Метод заполнения массивов</param>
        /// <param name="maxSize">максимальная величина массива</param>
        /// <param name="message">сообщение</param>
        private void ExecuteSort(Action <IArray <long>, int> Fill, int maxSize, string message)
        {
            Console.WriteLine($"{message} method");
            IArray <long> arr1;

            arr1 = new BubbleArray <long>(maxSize);
            Fill(arr1, maxSize);
            SortArr(arr1, "Buble sort " + message);

            arr1 = new SelectedArray <long>(maxSize);
            Fill(arr1, maxSize);
            SortArr(arr1, "Selected sort " + message);

            arr1 = new InsertSort <long>(maxSize);
            Fill(arr1, maxSize);
            SortArr(arr1, "Insert sort " + message);

            arr1 = new MergeSort <long>(maxSize);
            Fill(arr1, maxSize);
            SortArr(arr1, "Merge sort " + message);

            arr1 = new ShellSort <long>(maxSize);
            Fill(arr1, maxSize);
            SortArr(arr1, "Shell sort " + message);

            arr1 = new QuickSort(maxSize);
            Fill(arr1, maxSize);
            SortArr(arr1, "Quick sort " + message);
            Console.WriteLine();
        }
Exemplo n.º 4
0
        private static void TrySort()
        {
            SortBase sort = new InsertSort();

            sort.Print("-------- before insert sort ----------");
            sort.DoSort();
            sort.Print("-------- after insert sort ----------");

            SortBase ms = new MergeSort();

            ms.Print("-------- before merge sort ----------");
            ms.DoSort();
            ms.Print("-------- after merge sort ----------");

            SortBase hs = new HeapSort();

            hs.Print("-------- before heap sort ----------");
            hs.DoSort();
            hs.Print("-------- after heap sort ----------");

            QuickSort qs = new QuickSort();

            qs.Print("-------- before quick sort ----------");
            qs.DoSort();
            qs.Print("-------- after quick sort ----------");

            CountingSort cs = new CountingSort();

            cs.Print("-------- before quick sort ----------");
            cs.DoSort();
            cs.Print("-------- after quick sort ----------");
        }
Exemplo n.º 5
0
        static void Main()
        {
            //лист пользовательских типов
            IEnumerable<Person> personList = new List<Person>
            {
                new Person { BirthYear = 1948, Name = "Cat Stevens" },
                new Person { BirthYear = 1955, Name = "Kevin Costner" },
                new Person { BirthYear = 1952, Name = "Vladimir Putin" },
                new Person { BirthYear = 1955, Name = "Bill Gates" },
                new Person { BirthYear = 1948, Name = "Kathy Bates" },
                new Person { BirthYear = 1956, Name = "David Copperfield" },
                new Person { BirthYear = 1948, Name = "Jean Reno" }
            };

            personList = new MergeSort().Sort(personList, (x, y) => x.BirthYear.CompareTo(y.BirthYear));

            //массив чисел
            IEnumerable<int> intArray = new[] {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};

            intArray = new ShellSort().Sort(intArray, Comparer<int>.Default);

            IEnumerable<KeyValuePair<string, int>> personDictionary = new Dictionary<string, int>
            {
                {"Cat Stevens", 1948},
                {"Kevin Costner", 1955},
                {"Vladimir Putin", 1952},
                {"Bill Gates", 1955},
                {"Kathy Bates", 1948},
                {"David Copperfield", 1954},
                {"Jean Reno", 1948}
            };
            personDictionary = new InsertSort().Sort(personDictionary, (x, y) => x.Value.CompareTo(y.Value));
        }
Exemplo n.º 6
0
        public void TestInsertSort()
        {
            var sort = new InsertSort();

            sort.Sort(_arr);
            CollectionAssert.AreEqual(_arr, _expectedArr);
        }
Exemplo n.º 7
0
        public void SortTest()
        {
            var arr        = new int[] { 3, 6, 7, 4, 9 };
            var insertSort = new InsertSort();

            insertSort.Sort(arr, SortDirection.Desc);
        }
Exemplo n.º 8
0
        private void InsertSort_Click(object sender, EventArgs e)
        {
            panel4.Controls.Clear();
            var listForSort = new InsertSort <SortedItem>(items);
            var NewItems    = DisplayList(listForSort.Items);

            listForSort = new InsertSort <SortedItem>(NewItems);
            StartSorting(listForSort);
        }
Exemplo n.º 9
0
        static void Main(string[] args)
        {
            ISort insertSort = new InsertSort();
            var   list       = insertSort.Sort(new int[] { 5, 2, 4, 6, 1, 3 });

            foreach (var number in list)
            {
                Console.Write(number + " ");
            }
        }
Exemplo n.º 10
0
        public void InsertSortTest()
        {
            int[]      input      = new int[] { 5, 4, 10, 9, 8, 3, 2, 7, 6, 1 };
            InsertSort insertSort = new InsertSort();

            insertSort.Sort <int>(input);
            for (int i = 0; i < input.Length; i++)
            {
                Assert.IsTrue(input[i] == i + 1);
            }
        }
Exemplo n.º 11
0
        static void SortAlgorithm(int[] array)
        {
            BubbleSort.Sort(array);
            Console.WriteLine($"冒泡排序结果:{string.Join(",", array)}");

            QuickSort.Sort(array, 0, array.Length - 1);
            Console.WriteLine($"快速排序结果:{string.Join(",", array)}");

            InsertSort.Sort(array);
            Console.WriteLine($"插入排序结果:{string.Join(",", array)}");
        }
Exemplo n.º 12
0
        public void InsertSortTest()
        {
            var insert = new InsertSort <int>();

            insert.Items.AddRange(items);
            insert.Sort();
            for (int i = 0; i < items.Count; i++)
            {
                Assert.AreEqual(sorted[i], insert.Items[i]);
            }
        }
Exemplo n.º 13
0
        public void Testing_Insertion_Sort()
        {
            //arrange
            int[] input    = new int[] { 5, 2, 9, 4 };
            int[] expected = new int[] { 2, 4, 5, 9 };

            //act
            int[] actual = InsertSort.InsertionSort(input);

            //assert
            Assert.Equal(expected, actual);
        }
Exemplo n.º 14
0
        public void Empty_Input_Throws_Error()
        {
            //arrange
            int[] input = new int[0];

            //assert
            Assert.Throws <EmptyArrayException>(() =>
            {
                //act
                var actual = InsertSort.InsertionSort(input);
            });
        }
Exemplo n.º 15
0
        //输入数字按回车进入对应的排序方法
        public static void SelectASort()
        {
            string i = Console.ReadLine().Trim();

            switch (i)
            {
            case "1":
            {
                Console.WriteLine("<*******冒泡排序法*******>");
                int[] numbers       = SortUtil.Read();
                int[] bubblenumbers = BubbleSort.sort(numbers);
                SortUtil.Write(bubblenumbers);
                break;
            }

            case "2":
            {
                Console.WriteLine("<*******插入排序法*******>");
                int[] numbers       = SortUtil.Read();
                int[] insertnumbers = InsertSort.sort(numbers);
                SortUtil.Write(insertnumbers);
                break;
            }

            case "3":
            {
                Console.WriteLine("<*******希尔排序法*******>");
                int[] numbers      = SortUtil.Read();
                int[] shellnumbers = ShellSort.sort(numbers);
                SortUtil.Write(shellnumbers);
                break;
            }

            case "4":
            {
                Console.WriteLine("<*******简单排序法*******>");
                int[] numbers       = SortUtil.Read();
                int[] simplenumbers = SimpleSort.sort(numbers);
                SortUtil.Write(simplenumbers);
                break;
            }

            case "5":
            {
                Console.WriteLine("<*******归并排序法*******>");
                int[] numbers   = SortUtil.Read();
                int[] mergesort = MergeSort.sort(numbers);
                SortUtil.Write(mergesort);
                break;
            }
            }
        }
Exemplo n.º 16
0
    static void testInsertSort()
    {
        // test InsertSort;
        resetNumber();
        //  开始监视代码运行时间;
        Stopwatch stopwatch = new Stopwatch();

        stopwatch.Start();
        InsertSort.Sort(mNumbers);
        stopwatch.Stop();
        Console.WriteLine("test InsertSort=> {0}毫秒", stopwatch.Elapsed.TotalMilliseconds);
        TestDebug.log(mNumbers);
    }
Exemplo n.º 17
0
        public void SortTestInsert()
        {
            InsertSort <Int32> insert = new InsertSort <Int32>();

            insert.Items.AddRange(list);

            insert.Sort();

            for (Int32 i = 0; i < sorted.Length; i++)
            {
                Assert.AreEqual(sorted[i], insert.Items[i]);
            }
        }
Exemplo n.º 18
0
        public void InsertSortTest()
        {
            int[] result = CreateResultArray();

            InsertSort <int> sort = new InsertSort <int>(result);

            result = sort.Sort();

            for (int i = 0; i < array.Length; i++)
            {
                Assert.AreEqual(expected[i], result[i]);
            }
        }
Exemplo n.º 19
0
        public void SelectSortTest()
        {
            int[] testArray         = ArrayOperations.GenerateIntegerArray(999);
            int[] selectSortedArray = InsertSort.Sort(testArray);

            // iterate across sorted array, verifying that it is less than or equal to the next item in the array
            for (int i = 0; i < testArray.Length - 1; i++)
            {
                if (!(selectSortedArray[i] <= selectSortedArray[i + 1]))
                {
                    Assert.Fail($"Array at position {i} has failed validation for Selection Sort!!!");
                }
            }
        }
Exemplo n.º 20
0
        public void InsertSortTest()
        {
            // arrange
            var insert = new InsertSort <int>();

            // act
            insert.Items.AddRange(Items);
            insert.Sort();
            // assert
            for (int i = 0; i < insert.Items.Count; i++)
            {
                Assert.AreEqual(Sorted[i], insert.Items[i]);
            }
        }
Exemplo n.º 21
0
        static void template()
        {
            int[] nums = { 243, 5, 7, 22, 3, 11 };

            long paytime = 0;
            //SortBase sort=new BetterQuickSort1();
            //SortBase sort=new QuickSort();
            //SortBase sort=new BubblingSort();
            //SortBase sort=new SelectSort();
            SortBase sort = new InsertSort();

            paytime = sort.run(nums);
            Console.WriteLine("paytime=" + paytime);
        }
Exemplo n.º 22
0
        static void Main(string[] args)
        {
            int[] grandezza = { 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000, 30000, 50000, 100000, 150000, 200000, };
            using (StreamWriter sw = new StreamWriter("file.csv", false, Encoding.UTF8))
            {
                sw.WriteLine("Algoritmo;Dimensione;Tempo");
                foreach (int dim in grandezza)
                {
                    Console.WriteLine(dim);
                    int[]  array = new int[dim];
                    Random r     = new Random();
                    for (int x = 0; x < dim; x++)
                    {
                        array[x] = r.Next(0, 100);
                    }
                    Stopwatch s = new Stopwatch();
                    s.Restart();
                    BubbleSort.sort(Copia(array));
                    s.Stop();
                    long temp = s.ElapsedMilliseconds;
                    sw.WriteLine($"BubbleSort;{dim};{temp}");

                    s.Restart();
                    InsertSort.Sort(Copia(array));
                    s.Stop();
                    temp = s.ElapsedMilliseconds;
                    sw.WriteLine($"InsertionSort;{dim};{temp}");

                    s.Restart();
                    InsertSort.Sort(Copia(array));
                    s.Stop();
                    temp = s.ElapsedMilliseconds;
                    sw.WriteLine($"SelectionSort;{dim};{temp}");


                    s.Restart();
                    Array.Sort(Copia(array));
                    s.Stop();
                    temp = s.ElapsedMilliseconds;
                    sw.WriteLine($"ArraySort;{dim};{temp}");

                    s.Restart();
                    MergeSort.Sort(Copia(array));
                    s.Stop();
                    temp = s.ElapsedMilliseconds;
                    sw.WriteLine($"MergeSort;{dim};{temp}");
                }
                sw.Flush();
            }
        }
Exemplo n.º 23
0
 private void InsertSortButtonClick(object sender, EventArgs e)
 {
     if (progressBarCount < 2)
     {
         MessageBox.Show("нечего сортировать");
     }
     else
     {
         var insert = new InsertSort <int>();
         insert.Items.AddRange(values.Select(x => x.Bar.Value));
         insert.SwapEvent += SwapEvent;
         var time = insert.Sort();
         ShowInfoAboutSort(insert, time);
     }
 }
Exemplo n.º 24
0
        static void PerformCountSort(int sizeArray)
        {
            SortingAlgorithms sorting = new InsertSort(sizeArray);

            sorting.FillArray();
            Stopwatch watch = new Stopwatch();

            watch.Start();
            sorting.SortArray();
            watch.Stop();
            var arr = sorting.GetSortedArray;

            Print(arr);
            Console.WriteLine($"Total Execution Time: {watch.ElapsedMilliseconds} ms");
        }
Exemplo n.º 25
0
        static void Main(string[] args)
        {
            var list = new IComparable[] { 1, 10, 6, 13, 4, 3, 2, 1, 1, 6, 5, 3, 7, 4, 1 }.ToList();

            InsertSort insAlg = new InsertSort();

            Console.WriteLine(insAlg.Sort(list).Render());
            Console.ReadKey();

            list = new IComparable[] { 1, 15, 56, 0, -2, 4, 2, 7, 4, 7, 34, 1, 23, 2, 6, 4, 1, 12 }.ToList();

            MergeSort mergeAlg = new MergeSort();

            System.IO.File.WriteAllText
                (@"C:\temp\growth.dat", mergeAlg.Growth(20000, 100).Render());
            Console.ReadKey();
            Console.ReadKey();
        }
Exemplo n.º 26
0
        static void Main(string[] args)
        {
            double[]      d     = { 2, 3, 1, 9, 8, 4, 10 };
            List <double> listd = new List <double>();

            foreach (double j in d)
            {
                listd.Add(j);
            }

            sortMethod sortTest = new InsertSort();

            sortTest.sort(ref listd);

            foreach (double num in listd)
            {
                Console.WriteLine(num);
            }

            Console.ReadKey();
        }
Exemplo n.º 27
0
        public void SortTest()
        {
            //arrenge
            var insert = new InsertSort <int>();
            var rnd    = new Random();
            var items  = new List <int>();

            for (int i = 0; i < 15; i++)
            {
                items.Add(rnd.Next(0, 100));
            }
            insert.Items.AddRange(items);
            var sorted = items.OrderBy(x => x).ToArray();//стадартная сортировка List

            //act
            insert.Sort();
            //assert
            for (int i = 0; i < items.Count; i++)
            {
                Assert.AreEqual(sorted[i], insert.Items[i]);
            }
        }
Exemplo n.º 28
0
 public void test_sorted_with_change_couple()
 {
     int[] array = { 1, 2, 3, 5, 4, 6, 7 };
     InsertSort.insert_sort(array);
     CollectionAssert.AreEqual(result_array, array);
 }
Exemplo n.º 29
0
 public void test_sorted_with_change_firstlast()
 {
     int[] array = { 7, 2, 3, 4, 5, 6, 1 };
     InsertSort.insert_sort(array);
     CollectionAssert.AreEqual(result_array, array);
 }
Exemplo n.º 30
0
 public void test_random()
 {
     int[] array = { 3, 5, 1, 4, 7, 2, 6 };
     InsertSort.insert_sort(array);
     CollectionAssert.AreEqual(result_array, array);
 }
Exemplo n.º 31
0
 public void test_sorted_reverse()
 {
     int[] array = { 7, 6, 5, 4, 3, 2, 1 };
     InsertSort.insert_sort(array);
     CollectionAssert.AreEqual(result_array, array);
 }