Sort() public method

利用希尔排序将数组按升序排序。
public Sort ( Array, a ) : void
a Array, 需要排序的数组。
return void
        public void Test()
        {
            ShellSort <Int32> sort = new ShellSort <Int32>();

            Int32[] array = SortData.CreateUnsortedArray();

            sort.Sort(array);

            Assert.True(SortData.VerifyArrayIsSorted(array));
        }
        public void TestCaseDescending()
        {
            SampleArray <int> .Get(out int[] arr, out int[] arrCopy, () => _random.Next(0, 100));

            Array.Sort(arrCopy, new Comparison <int>((x, y) => y.CompareTo(x)));
            var sort = new ShellSort <int>(Comparer <int> .Create(new Comparison <int>((x, y) => y.CompareTo(x))));

            sort.Sort(arr);
            Assert.Equal(arr, arrCopy);
        }
Example #3
0
        static void Main(string[] args)
        {
            DemailChanges();
            return;

            MathsPrograms();
            return;

            //int[] unSortedArray = new[] {3, 5, 1, 4, 7, 2, 8, 9, 10,3};
            int[] unSortedArray = new[] { 23, 29, 15, 19, 31, 7, 9, 5, 2 };
            Console.WriteLine("unsorted Array");
            unSortedArray.ToList().ForEach(x => Console.Write(x.ToString() + " "));
            Console.WriteLine("\n Press number for following: \n");
            Console.WriteLine("Press 1 for Selection Sort \n");
            Console.WriteLine("Press 2 for Bubble Sort \n");
            Console.WriteLine("Press 3 for Merge Sort \n");
            Console.WriteLine("Press 4 for Quick Sort \n");
            Console.WriteLine("Press 5 for Insertion Sort\n");
            Console.WriteLine("Press 6 for Bucket Sort\n");
            Console.WriteLine("Press 7 for Shell Sort\n");

            string   number = Console.ReadLine();
            int      result;
            ISorting sort = new SelectionSort();

            if (!int.TryParse(number, out result) && (result > 0 && result <= 6))
            {
                Console.WriteLine("Provide the input from 1 to 6");
            }
            switch (number)
            {
            case "1":
                sort = new SelectionSort();
                sort.Sort(unSortedArray);
                break;

            case "2":
                sort = new BubbleSort();
                sort.Sort(unSortedArray);
                break;

            case "5":
                sort = new InsertionSort();
                sort.Sort(unSortedArray);
                break;

            case "7":
                sort = new ShellSort();
                sort.Sort(unSortedArray);
                break;
            }

            unSortedArray.ToList().ForEach(x => Console.Write(x.ToString() + " "));
            Console.ReadKey();
        }
Example #4
0
    private static void SortAllTabsFromList(List <int[]> list, int mode)
    {
        Time[] timeArray = new Time[list.Count];     //Tworzenie tablicy obiektów Time (Każdy pojedynczy obiekt klasy Time przechowuje informacje która tablica była sortowana i w jakim czasie)

        Console.WriteLine($"Sorting list of tabs with length {list[0].Length}");

        Stopwatch watch = new Stopwatch();     //Tworzenie obiektu klasy Stopwatch która będzie liczyć czas sortowania

        //kazda tablica z listy jest sortowana
        for (int i = 0; i < list.Count; i++)
        {
            watch.Start();     //Rozpoczęcie odliczania czasu pojedynczego sortowania
            switch (mode)
            {
            case 1:
                QuickSort.Sort(list[i], 0, list[i].Length - 1);
                break;

            case 2:
                ShellSort.Sort(list[i], list[i].Length);
                break;

            case 3:
                HeapSort.Sort(list[i], list[i].Length);
                break;
            }
            watch.Stop();     //Koniec odliczania czasu pojedynczego sortowania

            Console.WriteLine($"Tab nr {i} sort time {watch.ElapsedMilliseconds}");
            Time time = new Time()
            {
                tabNumber = i, time = watch.ElapsedMilliseconds
            };                   //Tworzę nowy obiekt klasy Time, zapisuje w nim informację o tym która tablica była sortowana oraz czas sortowania
            timeArray[i] = time; //Dodaje ten obiekt Time do tablicy Time'ów która na końcu będzie zapisana do pliku XML
            watch.Reset();       //Po każdej iteracji resetuje czas odliczania
        }

        XmlSerializer serializer = new XmlSerializer(typeof(TimeToSave));     /*Tworzę nowy obiekt klasy XmlSerializer
                                                                               * która posłuży do zapisania danych o czasie sortowań do pliku XML (w argumencie przekazuje typ obiektu jaki będzie zapisany do pliku XML)
                                                                               * czyli TimeToSave, w nim przechowam tablicę Time'ów którą stworzyłem na samym początku tej metody ("Time[] timeArray = new Time[list.Count]")
                                                                               */

        TimeToSave timeArrayToSave = new TimeToSave()
        {
            time = timeArray
        };                                                                    //Tworzę obiekt TimeToSave i zapisuje w nim tablicę Time'ów

        //Zapisuje czasy sortowań do pliku XML, blok using sprawi że po zapisie, strumienie zostaną zamknięte dzięki czemu nie będzie problemów później z korzystaniem z pliku XML
        using (TextWriter writer = new StreamWriter($"TabsSortTimeLength{list[0].Length}.xml"))
        {
            serializer.Serialize(writer, timeArrayToSave);
        }

        Console.WriteLine($"List is sorted");
    }
Example #5
0
        public static void Main()
        {
            var elements = Console.ReadLine()
                           .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                           .Select(int.Parse)
                           .ToArray();

            ShellSort <int> .Sort(elements, elements.Length);

            Console.WriteLine(string.Join(" ", elements));
        }
Example #6
0
        public void ShellSortTest()
        {
            var shell = new ShellSort <int>();

            shell.Items.AddRange(items);
            shell.Sort();
            for (int i = 0; i < items.Count; i++)
            {
                Assert.AreEqual(sorted[i], shell.Items[i]);
            }
        }
        public void SortNormally()
        {
            var array = new[] { 25, 17, 49, 1, 195, 58 };

            var shellSort = new ShellSort(array);

            var result = shellSort.Sort().ToList();

            Assert.AreEqual(1, result.First());
            Assert.AreEqual(195, result.Last());
        }
Example #8
0
        public void ShouldBeSorted(int[] input, string expect)
        {
            //Arrange
            var sut = new ShellSort(); //sut: system under test

            //Act
            var result = sut.Sort(input);

            //Assert
            Assert.Equal(expect, string.Join(',', result));
        }
Example #9
0
        public void ShellSort_GivenUnsortedArray_SortedArray()
        {
            // Arrange
            int[] unsorted       = new int[] { 4, 3, 2, 1, 5, 3 };
            int[] expectedResult = new int[] { 1, 2, 3, 3, 4, 5 };

            // Act
            ShellSort.Sort(unsorted);

            // Assert
            CollectionAssert.AreEqual(unsorted, expectedResult);
        }
Example #10
0
        public void ShellSortTest()
        {
            int[] result = CreateResultArray();

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

            result = sort.Sort();

            for (int i = 0; i < array.Length; i++)
            {
                Assert.AreEqual(expected[i], result[i]);
            }
        }
Example #11
0
        public void TestShellSort()
        {
            var result = new int[_size];

            Array.Copy(_unsorted, result, _size);

            ShellSort.Sort(result);

            for (int i = 0; i < result.Length; i++)
            {
                Assert.AreEqual(_sorted[i], result[i]);
            }
        }
        public void ShellSortTest()
        {
            //arrange
            var ShellSort = new ShellSort <int>(dataList, "Shell");

            //act
            ShellSort.Sort();
            //assert
            for (int i = 0; i < count; i++)
            {
                Assert.AreEqual(sortedItems[i], ShellSort.Items[i]);
            }
        }
Example #13
0
        public void SortTestShell()
        {
            ShellSort <Int32> shell = new ShellSort <Int32>();

            shell.Items.AddRange(list);

            shell.Sort();

            for (Int32 i = 0; i < sorted.Length; i++)
            {
                Assert.AreEqual(sorted[i], shell.Items[i]);
            }
        }
        public void SortWithComparerTest_DESC()
        {
            Point[] points = new Point[10];
            for (int i = 0; i < 10; i++)
            {
                points[i] = new Point(i, 10 - i);
            }
            Shuffle.Do(points);

            Assert.IsFalse(SortHelper.IsSorted(points, Point.X_ORDER, SortOrder.DESC));
            ShellSort.Sort(points, Point.X_ORDER, SortOrder.DESC);
            Assert.IsTrue(SortHelper.IsSorted(points, Point.X_ORDER, SortOrder.DESC));
        }
        public void ShellSortTest()
        {
            int[] unsorted = DataManager.GetUnsortedData();
            int[] sorted   = DataManager.GetSortedData();

            ShellSort sort = new ShellSort();

            sort.Sort(unsorted);
            for (int i = 0; i < unsorted.Length; i++)
            {
                Assert.AreEqual(unsorted[i], sorted[i]);
            }
        }
Example #16
0
        private void btnC4_Click(object sender, EventArgs e)
        {
            Ordenado      = true;
            ListaAuxiliar = new List <int>(Lista);
            var t1 = DateTime.Now;

            ShellSort.Sort(ListaAuxiliar);
            var t2 = DateTime.Now;

            label4.Text = "Shell Sort = " + (t2 - t1).Milliseconds.ToString() + " ms";
            CarregaGrid(ListaAuxiliar);
            VerificaBloqueiaCalcular();
            btnDesord.Enabled = true;
        }
Example #17
0
        public void ShellSortTest()
        {
            // arrange
            var shell = new ShellSort <int>();

            shell.Items.AddRange(Items);
            // act
            shell.Sort();
            // assert
            for (int i = 0; i < shell.Items.Count; i++)
            {
                Assert.AreEqual(Sorted[i], shell.Items[i]);
            }
        }
        public void Test(int[] data)
        {
            // Arrange
            int[] data2 = new int[data.Length];
            Array.Copy(data, data2, data.Length);
            Array.Sort(data2);
            var sorter = new ShellSort <int>();

            // Act
            sorter.Sort(data);

            // Assert
            CollectionAssert.AreEqual(data, data2);
        }
Example #19
0
 private void ShellSortButtonClick(object sender, EventArgs e)
 {
     if (progressBarCount < 2)
     {
         MessageBox.Show("нечего сортировать");
     }
     else
     {
         var shell = new ShellSort <int>();
         shell.Items.AddRange(values.Select(x => x.Bar.Value));
         shell.SwapEvent += SwapEvent;
         var time = shell.Sort();
         ShowInfoAboutSort(shell, time);
     }
 }
Example #20
0
        public void testSort()
        {
            var a = new int[100];
            var b = new int[100];

            for (var i = 0; i < a.Length; ++i)
            {
                a[i] = i;
                b[i] = i;
            }
            KnuthShuffle.Shuffle(a);
            Assert.NotEqual(b, a);
            ShellSort.Sort(a);
            Assert.Equal(b, a);
        }
        public void ShellSort_Descending_Stress_Test()
        {
            var rnd           = new Random();
            var nodeCount     = 1000;
            var randomNumbers = Enumerable.Range(1, nodeCount)
                                .OrderBy(x => rnd.Next())
                                .ToList();

            var result = ShellSort <int> .Sort(randomNumbers.ToArray(), SortDirection.Descending);

            for (int i = 0; i < nodeCount; i++)
            {
                Assert.AreEqual(randomNumbers.Count - i, result[i]);
            }
        }
        public void ShellSort_Stress_Test()
        {
            var rnd           = new Random();
            var nodeCount     = 1000;
            var randomNumbers = Enumerable.Range(1, nodeCount)
                                .OrderBy(x => rnd.Next())
                                .ToList();

            var result = ShellSort <int> .Sort(randomNumbers.ToArray());

            for (int i = 1; i <= nodeCount; i++)
            {
                Assert.AreEqual(i, result[i - 1]);
            }
        }
        public void ShellSortTest()
        {
            // arange
            var test = new ShellSort <int>();

            test.Items.AddRange(Items);

            // act
            test.Sort();

            // assert
            for (int i = 0; i < Sorted.Count; i++)
            {
                Assert.AreEqual(Sorted[i], test.Items[i]);
            }
        }
Example #24
0
        public static void ShellSort()
        {
            int[] arr;
            FileToArr(out arr);
            Console.WriteLine("- Danh sach ban dau lay tu File:");
            Console.WriteLine("[{0}]", string.Join(", ", arr));
            Console.WriteLine("- Danh sach sau khi duoc sap xep:");
            var sw2    = Stopwatch.StartNew();
            var sorter = new ShellSort();

            sorter.Sort(arr);
            sw2.Stop();
            Console.WriteLine("[{0}]", string.Join(", ", arr));
            Console.WriteLine("Time taken ShellSort: {0} ms", sw2.Elapsed.TotalMilliseconds);
            Console.ReadKey(true);
        }
Example #25
0
        public void SellSort1()
        {
            var shellSort = new ShellSort();

            Assert.IsTrue(shellSort.Add(43));
            Assert.IsTrue(shellSort.Add(7));
            Assert.IsTrue(shellSort.Add(11));
            Assert.IsTrue(shellSort.Add(68));
            Assert.IsTrue(shellSort.Add(55));
            Assert.IsTrue(shellSort.Add(22));
            Assert.IsTrue(shellSort.Add(102));
            Assert.IsTrue(shellSort.Add(3));

            shellSort.Sort();

            Assert.IsTrue(shellSort.IsSorted());
        }
Example #26
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();
            }
        }
Example #27
0
        public void ShellSortTest()
        {
            var sortedIntArray = _intArray.ToArray();

            ShellSort.Sort(sortedIntArray);
            for (var i = 0; i < sortedIntArray.Length; i++)
            {
                Assert.AreEqual(_sortedIntArray[i], sortedIntArray[i]);
            }

            var sortedStringArray = _stringArray.ToArray();

            ShellSort.Sort(sortedStringArray);
            for (var i = 0; i < sortedStringArray.Length; i++)
            {
                Assert.AreEqual(_sortedStringArray[i], sortedStringArray[i]);
            }
        }
Example #28
0
        public void ShellSortTest()
        {
            // Arrange
            var arrayCount = 0;

            foreach (var sample in GenerateSampleArrays())
            {
                // Act
                ShellSort.Sort(sample);

                // Assert
                for (int i = 0; i < sample.Length - 1; i++)
                {
                    Assert.True(sample[i] <= sample[i + 1], $"Array #{arrayCount} was not sorted.");
                    arrayCount++;
                }
            }
        }
        static void Main(string[] args)
        {
            Stopwatch stopwatch = new Stopwatch();
            ShellSort shellSort = new ShellSort();
            MergeSort mergeSort = new MergeSort();
            QuickSort quickSort = new QuickSort();

            int n             = 1000;
            int nMultipleBy10 = 4;

            for (int i = 0; i < nMultipleBy10; i++)
            {
                Console.WriteLine("n=" + n);
                Transaction[] trans = TransactionGenerator.Generate(n);

                Transaction[] testCase = new Transaction[n];

                trans.CopyTo(testCase, 0);
                stopwatch.Restart();
                shellSort.Sort(testCase);
                stopwatch.Stop();
                Console.WriteLine("Shell Sort: " + stopwatch.ElapsedMilliseconds + " ms");

                trans.CopyTo(testCase, 0);
                stopwatch.Restart();
                mergeSort.Sort(testCase);
                stopwatch.Stop();
                Console.WriteLine("Merge Sort: " + stopwatch.ElapsedMilliseconds + " ms");

                trans.CopyTo(testCase, 0);
                stopwatch.Restart();
                quickSort.Sort(testCase);
                stopwatch.Stop();
                Console.WriteLine("Quick Sort: " + stopwatch.ElapsedMilliseconds + " ms");

                trans.CopyTo(testCase, 0);
                stopwatch.Restart();
                Heap.Sort(testCase);
                stopwatch.Stop();
                Console.WriteLine("Heap Sort: " + stopwatch.ElapsedMilliseconds + " ms");

                n *= 10;
            }
        }
        public void SortTest()
        {
            //arrenge
            var shell = new ShellSort <int>();
            var rnd   = new Random();
            var items = new List <int>();

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

            //act
            shell.Sort();
            //assert
            for (int i = 0; i < items.Count; i++)
            {
                Assert.AreEqual(sorted[i], shell.Items[i]);
            }
        }
Example #31
0
 public static long getSortTime(SORT_TYPE type, int[] array)
 {
     var attempts = new long[Form1.ATTEMPTS];
     for (var j = 0; j < Form1.ATTEMPTS; j++)
     {
         switch (type)
         {
             case SORT_TYPE.SORT:
                 myStopwatch.Restart();
                 Array.Sort(array);
                 myStopwatch.Stop();
                 break;
             case SORT_TYPE.QUICKSORT:
                 Quicksort<int> q = new Quicksort<int>();
                 myStopwatch.Restart();
                 q.QSort(array);
                 myStopwatch.Stop();
                 break;
             case SORT_TYPE.HEAPSORT:
                 Heapsort h = new Heapsort();
                 myStopwatch.Restart();
                 h.heapSort(array);
                 myStopwatch.Stop();
                 break;
             case SORT_TYPE.SHELLSORT:
                 ShellSort<int> s = new ShellSort<int>();
                 myStopwatch.Restart();
                 s.Sort(array);
                 myStopwatch.Stop();
                 break;
             default:
                 throw new Exception("Unknown error");
         }
         attempts[j] = myStopwatch.ElapsedMilliseconds;
     }
     Array.Sort(attempts);
     return attempts[Form1.ATTEMT_NUMBER_TO_USE-1];
 }