Пример #1
0
        public void Test1()
        {
            var list = new List <int> {
                8, 5, 1, 2, 5, 4, 7, 5, 2, 1, 2, 1
            };

            CountingSorter.CountingSort(list);
        }
Пример #2
0
        public void ShouldSortSingleItem()
        {
            var sorter = new CountingSorter();

            int[] result = sorter.CountingSort(new int[] { 3 }, 5);

            Assert.AreEqual(3, result[0]);
        }
Пример #3
0
        public void ShouldCreateSameSizedResult()
        {
            var sorter = new CountingSorter();

            int[] result = sorter.CountingSort(new int[] { 3, 1, 5, 4 }, 5);

            Assert.AreEqual(4, result.Length);
        }
Пример #4
0
        public void ShouldNotAllowItemsOutOfRange()
        {
            var sorter = new CountingSorter();

            int[] items = { 3, 1, 5, 4 };

            Assert.Throws <ArgumentOutOfRangeException>(() => sorter.CountingSort(items, 4));
        }
Пример #5
0
        public void ShouldSortEmptyArray()
        {
            var sorter = new CountingSorter();

            int[] result = sorter.CountingSort(new int[] { }, 5);

            Assert.IsEmpty(result);
        }
Пример #6
0
        public void ShouldSortMultipleItemsWithRepeats()
        {
            var sorter = new CountingSorter();

            int[] result = sorter.CountingSort(new int[] { 4, 3, 4, 5, 1, 1, 5, 4 }, 5);

            int[] expected = { 1, 1, 3, 4, 4, 4, 5, 5 };
            Assert.AreEqual(expected, result);
        }
Пример #7
0
 public void RunTests(CountingSorter countingSorter)
 {
     int[] a        = new int[] { 4, 7, 12, 8, 2, 4, 23, 21 };
     int[] expected = new int[] { 2, 4, 4, 7, 8, 12, 21, 23 };
     int[] result   = countingSorter.Sort(a);
     for (int i = 0; i < a.Length; i++)
     {
         Assert.AreEqual(expected[i], result[i]);
     }
 }
Пример #8
0
        public static void SortsArray([Random(0, 10000, 100, Distinct = true)] int n)
        {
            // Arrange
            var(correctArray, testArray) = RandomHelper.GetArrays(n);

            // Act
            CountingSorter.Sort(testArray);
            Array.Sort(correctArray);

            // Assert
            Assert.AreEqual(correctArray, testArray);
        }
Пример #9
0
        public void Sort_DoesNotChange_OriginalInput()
        {
            //arrange
            const int firstElement = 5, lastElement = 3;
            var       original = new List <int> {
                firstElement, 2, 4, 6, 1, lastElement
            };
            var sorter = new CountingSorter();
            //act
            var sorted = sorter.Sort(original);

            //assert
            original[0].Should().Be(firstElement);
            original[5].Should().Be(lastElement);
        }
Пример #10
0
        public int[] Sort(int[] a)
        {
            if (a == null)
            {
                return(null);
            }

            int            max = GetMaximumValue(a);
            int            significantDigit = (int)Math.Log10(max);
            CountingSorter countingSorter   = new CountingSorter();

            for (int i = 0; i < significantDigit; i++)
            {
                a = countingSorter.Sort(a, i);
            }
            return(a);
        }
Пример #11
0
        public void Sort_Returns_AscOrderedCollection()
        {
            //arrange
            var original = new List <int> {
                5, 2, 4, 6, 1, 3
            };
            var sorter = new CountingSorter();
            //act
            var sorted = sorter.Sort(original).ToList();
            //assert
            var prev = sorted[0];

            for (var i = 1; i < sorted.Count; i++)
            {
                prev.Should().BeLessOrEqualTo(sorted[i]);
                prev = sorted[i];
            }
        }
Пример #12
0
        public static void SortsEmptyArray()
        {
            var sorter = new CountingSorter();

            sorter.Sort(Array.Empty <int>());
        }
Пример #13
0
        public void CountingSorterTest()
        {
            CountingSorter countingSorter = new CountingSorter();

            RunTests(countingSorter);
        }
Пример #14
0
        static void SortDemo()
        {
            // seven custom algorithms;
            // for each algorithm prepare copies of the same array to sort
            Random rd = new Random();

            int[] z1 = new int[50000];
            for (int i = 0; i < z1.Length; i++)
            {
                z1[i] = rd.Next(0, z1.Length);
            }

            int[] z2 = new int[z1.Length];
            z1.CopyTo(z2, 0);

            int[] z3 = new int[z1.Length];
            z1.CopyTo(z3, 0);

            int[] z4 = new int[z1.Length];
            z1.CopyTo(z4, 0);

            int[] z5 = new int[z1.Length];
            z1.CopyTo(z5, 0);

            int[] z6 = new int[z1.Length];
            z1.CopyTo(z6, 0);

            int[] z7 = new int[z1.Length];
            z1.CopyTo(z7, 0);
            // ----------------------------------------------------------

            Console.WriteLine("Selection Sort:");

            ISorter <int> sorter = new SelectionSorter();

            DateTime dt1 = DateTime.Now;

            sorter.Sort(z1, 0, z1.Length - 1);
            DateTime dt2 = DateTime.Now;

            Console.WriteLine("Time: {0}:{1}", (dt2 - dt1).Seconds, (dt2 - dt1).Milliseconds);

            Console.WriteLine("First 20 elements:");
            foreach (int elem in z1.Take(20))
            {
                Console.Write(elem + " ");
            }


            Console.WriteLine();
            Console.WriteLine("Bubble sort");

            sorter = new BubbleSorter();

            dt1 = DateTime.Now;
            sorter.Sort(z2, 0, z1.Length - 1);
            dt2 = DateTime.Now;
            Console.WriteLine("{0}:{1}", (dt2 - dt1).Seconds, (dt2 - dt1).Milliseconds);

            Console.WriteLine("First 20 elements:");
            foreach (int elem in z2.Take(20))
            {
                Console.Write(elem + " ");
            }


            Console.WriteLine();
            Console.WriteLine("Insertion sort");

            sorter = new InsertionSorter();

            dt1 = DateTime.Now;
            sorter.Sort(z3, 0, z1.Length - 1);
            dt2 = DateTime.Now;
            Console.WriteLine("{0}:{1}", (dt2 - dt1).Seconds, (dt2 - dt1).Milliseconds);

            Console.WriteLine("First 20 elements:");
            foreach (int elem in z3.Take(20))
            {
                Console.Write(elem + " ");
            }


            Console.WriteLine();
            Console.WriteLine("Shaker sort");

            sorter = new ShakerSorter();

            dt1 = DateTime.Now;
            sorter.Sort(z4, 0, z1.Length - 1);
            dt2 = DateTime.Now;
            Console.WriteLine("{0}:{1}", (dt2 - dt1).Seconds, (dt2 - dt1).Milliseconds);

            Console.WriteLine("First 20 elements:");
            foreach (int elem in z4.Take(20))
            {
                Console.Write(elem + " ");
            }


            Console.WriteLine();
            Console.WriteLine("Shell sort");

            sorter = new ShellSorter();

            dt1 = DateTime.Now;
            sorter.Sort(z5, 0, z1.Length - 1);
            dt2 = DateTime.Now;
            Console.WriteLine("{0}:{1}", (dt2 - dt1).Seconds, (dt2 - dt1).Milliseconds);

            Console.WriteLine("First 20 elements:");
            foreach (int elem in z5.Take(20))
            {
                Console.Write(elem + " ");
            }


            Console.WriteLine();
            Console.WriteLine("Counting sort");

            sorter = new CountingSorter();

            dt1 = DateTime.Now;
            sorter.Sort(z6, 0, z1.Length - 1);
            dt2 = DateTime.Now;
            Console.WriteLine("{0}:{1}", (dt2 - dt1).Seconds, (dt2 - dt1).Milliseconds);

            Console.WriteLine("First 20 elements:");
            foreach (int elem in z6.Take(20))
            {
                Console.Write(elem + " ");
            }


            Console.WriteLine();
            Console.WriteLine("Quicksort");

            sorter = new QuickSorter();

            dt1 = DateTime.Now;
            sorter.Sort(z7, 0, z1.Length - 1);
            dt2 = DateTime.Now;
            Console.WriteLine("{0}:{1}", (dt2 - dt1).Seconds, (dt2 - dt1).Milliseconds);

            Console.WriteLine("First 20 elements:");
            foreach (int elem in z7.Take(20))
            {
                Console.Write(elem + " ");
            }

            Console.WriteLine();

            Console.ReadKey();
            Console.Clear();
        }