Ejemplo n.º 1
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]);
     }
 }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
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];
            }
        }
Ejemplo n.º 6
0
        public static void SortsEmptyArray()
        {
            var sorter = new CountingSorter();

            sorter.Sort(Array.Empty <int>());
        }
Ejemplo n.º 7
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();
        }