Exemplo n.º 1
0
    public void TestCountingSort()
    {
        CountingSort cs = new CountingSort();

        int[] a = new int[1000];
        int[] b = new int[100000];
        Array.Copy(smallTestIntegerArray, a, smallTestIntegerArray.Length);
        Array.Copy(bigTestIntegerArray, b, bigTestIntegerArray.Length);

        cs.Sort(a);
        Assert.IsTrue(CheckSort(a, (x, y) => x.CompareTo(y)));
        cs.Sort(b);
        Assert.IsTrue(CheckSort(b, (x, y) => x.CompareTo(y)));
    }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            Random r = new Random();

            Console.WriteLine("Inserisci la lunghezza dell'array: ");
            int n = int.Parse(Console.ReadLine());

            int[]      a = new int[n];
            int        numero;
            List <int> lista = new List <int>();

            for (int i = 0; i < a.Length; i++)
            {
                do
                {
                    numero = r.Next(0, n * 3);
                } while (lista.Contains(numero));
                lista.Add(numero);
                a[i] = numero;
            }
            a = CountingSort.Sort(a);
            Console.WriteLine("Array ordinato:");
            for (int i = 0; i < a.Length; i++)
            {
                Console.WriteLine(a[i]);
            }
            Console.ReadLine();
        }
Exemplo n.º 3
0
        public void SortingTest()
        {
            Action <int[]>[] actions = new Action <int[]>[]
            {
                BubbleSort.Sort,
                data => BucketSort.Sort(data, SortingTests.MaxValue),
                data => CountingSort.Sort(data, SortingTests.MaxValue),
                HeapSort.Sort,
                InsertionSort.Sort,
                MergeSort.Sort,
                QuickSort.Sort,
                data => RadixSort.Sort(data, SortingTests.MaxValue),
            };

            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 100; j++)
                {
                    int[]   data    = ArrayUtilities.CreateRandomArray(j, 0, SortingTests.MaxValue);
                    int[][] results = new int[actions.Length][];

                    for (int k = 0; k < actions.Length; k++)
                    {
                        results[k] = new int[data.Length];
                        Array.Copy(data, results[k], data.Length);

                        actions[k](results[k]);
                        Assert.IsTrue(ArrayUtilities.AreEqual(results[k], results[0]));
                    }
                }
            }
        }
Exemplo n.º 4
0
        public static void Show()
        {
            var sorter = new CountingSort();

            var numbers = Array.ConvertAll(FileUtilities.Read("cisla.txt"), int.Parse);

            ConsoleUtilities.Prompt("Probiha trideni dat...\n");

            sorter.Sort(numbers, numbers.Max(), numbers.Min());

            ConsoleUtilities.Prompt("Setrideno!\n");

            ConsoleUtilities.Prompt("Probiha zapis do souboru...\n");

            FileUtilities.DeleteIfExists(path);

            var builder = new StringBuilder();

            foreach (var number in numbers)
            {
                builder.AppendLine(number.ToString());
            }

            FileUtilities.Write(path, builder.ToString().Trim());

            ConsoleUtilities.Prompt("Hotovo!\n");
        }
Exemplo n.º 5
0
        public void CountingSort_Stress_Test()
        {
            int[] randomNumbers;
            int   nodeCount = 1000 * 1000;
            var   rnd       = new Random();

            randomNumbers = Enumerable.Range(1, nodeCount)
                            .OrderBy(x => rnd.Next())
                            .ToArray();

            var timer = new Stopwatch();

            timer.Start();

            var result = CountingSort.Sort(randomNumbers);

            timer.Stop();

            Debug.WriteLine($"sorted {nodeCount} integers using counting sort in {timer.ElapsedMilliseconds} milliseconds.");

            for (int i = 1; i <= nodeCount; i++)
            {
                Assert.AreEqual(i, result[i - 1]);
            }
        }
Exemplo n.º 6
0
        static void Main(string[] args)
        {
            Random r = new Random();

            Console.WriteLine("Inserisci la lunghezza dell'array: ");
            int n = int.Parse(Console.ReadLine());

            int[] a = new int[n];
            int   numero;

            for (int i = 0; i < a.Length; i++)
            {
                numero = r.Next(1, 50);
                a[i]   = numero;
            }
            Console.WriteLine("Array randomico:");
            for (int i = 0; i < a.Length; i++)
            {
                Console.Write(a[i] + " ");
            }
            a = CountingSort.Sort(a);
            Console.WriteLine("\nArray ordinato:");
            for (int i = 0; i < a.Length; i++)
            {
                Console.Write(a[i] + " ");
            }
            Console.WriteLine("\nLa complessità è n^2.");
            Console.ReadLine();
        }
        public void TestSort()
        {
            var arr = new[] { 3, 41, 52, 26, 38, 57, 9, 49 };

            CountingSort.Sort(arr);
            Assert.IsTrue(arr.SequenceEqual(new[] { 3, 9, 26, 38, 41, 49, 52, 57 }));
        }
        public void SortNegativeTest()
        {
            int[] data     = new int[] { 0, 8, 8, -2, 5, 5, 5, 1, 2, 3, -1, -1, 4, 4, 4, 10 };
            int[] expected = new int[] { -2, -1, -1, 0, 1, 2, 3, 4, 4, 4, 5, 5, 5, 8, 8, 10 };
            var   result   = CountingSort.Sort(data);

            CollectionAssert.AreEqual(expected, result, "CountingSort did not sort correctly");
        }
Exemplo n.º 9
0
        public void SortTest_MultipleOccurance_Gap()
        {
            int[]        numberArray = new int[] { 5, 5, 10, 10, 7, 8, 20, 1, 0 };
            CountingSort sorter      = new CountingSort();

            int[] returnArray = sorter.Sort(numberArray);
            CheckIfInOrder(returnArray);
        }
Exemplo n.º 10
0
        public void SortTest_SingleOccurance_Gap()
        {
            int[]        numberArray = new int[] { 4, 2, 9, 19, 500 };
            CountingSort sorter      = new CountingSort();

            int[] returnArray = sorter.Sort(numberArray);
            CheckIfInOrder(returnArray);
        }
Exemplo n.º 11
0
        public void SortTest_SingleOccurance_NoGap()
        {
            int[]        numberArray = new int[] { 4, 2, 3, 1, 6, 0, 5, 7, 9, 8 };
            CountingSort sorter      = new CountingSort();

            int[] returnArray = sorter.Sort(numberArray);
            CheckIfInOrder(returnArray);
        }
Exemplo n.º 12
0
        public CountingSortTests()
        {
            var sort = new CountingSort <int>();

            func      = array => sort.Sort(array);
            this.sort = sort;
            algorithm = nameof(CountingSort <int>);
        }
        public void SortEmptyTest()
        {
            int[] data     = new int[] { };
            int[] expected = new int[] { };
            var   result   = CountingSort.Sort(data);

            CollectionAssert.AreEqual(expected, result, "CountingSort did not sort correctly");
        }
        /// <summary>
        /// Sorts input in ascending order using Counting sort technique
        /// This techique currently supports only positive integer sorting.
        /// </summary>
        /// <param name="sort">ISort</param>
        /// <param name="input">input data</param>
        /// <returns>The ascending ordered content</returns>
        public static string UseCounting(this ISorting sort, string input)
        {
            var countSort = new CountingSort <char>(x => { return((int)x); });
            var charArray = input.ToCharArray();

            countSort.Sort(charArray);
            return(new string(charArray));
        }
        public void SortMaxTest()
        {
            int[] data     = new int[] { 8, 8, 5, 5, 5, 1, 2, 3, 4, 4, 4, 10 };
            int[] expected = new int[] { 1, 2, 3, 4, 4, 4, 5, 5, 5, 8, 8, 10 };
            var   result   = CountingSort.Sort(data, data.Max());

            CollectionAssert.AreEqual(expected, result, "CountingSort did not sort correctly");
        }
Exemplo n.º 16
0
        public void OnlyPositiveNumbers_CountingSort()
        {
            var countingSort = new CountingSort();
            var numbers      = new[] { 5, 9, 3, 9, 10, 9, 2, 4, 13, 10 };
            var expected     = new[] { 2, 3, 4, 5, 9, 9, 9, 10, 10, 13 };
            var result       = countingSort.Sort(numbers);

            Assert.AreEqual(expected, result);
        }
        public void CountingSort_Descending_Smoke_Test()
        {
            var result = CountingSort.Sort(testArray, SortDirection.Descending);

            for (int i = 0; i < testArray.Length; i++)
            {
                Assert.AreEqual(testArray.Length - i - 1, result[i]);
            }
        }
Exemplo n.º 18
0
        public void Test_CountingSort()
        {
            char[] arr    = { 'g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's' };
            var    actual = sort.Sort(arr);

            char[] expected = { 'e', 'e', 'e', 'e', 'f', 'g', 'g', 'k', 'k', 'o', 'r', 's', 's' };

            Assert.Equal(expected, actual);
        }
Exemplo n.º 19
0
        public void Sort()
        {
            int[]        arr          = new int[] { 80, 42, 99, 34, 54, 72, 78, 53, 1, 19, 28, 84, 3, 56, 64, 17, 86, 82, 17, 16, 2, 77, 83, 97, 30, 8, 35, 85, 40, 93, 54, 53, 3, 38, 19, 81, 74, 19, 6, 98, 5, 60, 72, 10, 32, 71, 40, 68, 1, 39, 11, 66, 68, 3, 88, 88, 87, 30, 34, 78, 74, 98, 47, 70, 13, 55, 82, 19, 43, 17, 96, 98, 63, 27, 95, 9, 13, 20, 47, 16, 95, 55, 29, 86, 44, 42, 67, 62, 100, 2, 17, 32, 99, 30, 75, 92, 43, 77, 39, 3 };
            int[]        resultArr    = new int[] { 1, 1, 2, 2, 3, 3, 3, 3, 5, 6, 8, 9, 10, 11, 13, 13, 16, 16, 17, 17, 17, 17, 19, 19, 19, 19, 20, 27, 28, 29, 30, 30, 30, 32, 32, 34, 34, 35, 38, 39, 39, 40, 40, 42, 42, 43, 43, 44, 47, 47, 53, 53, 54, 54, 55, 55, 56, 60, 62, 63, 64, 66, 67, 68, 68, 70, 71, 72, 72, 74, 74, 75, 77, 77, 78, 78, 80, 81, 82, 82, 83, 84, 85, 86, 86, 87, 88, 88, 92, 93, 95, 95, 96, 97, 98, 98, 98, 99, 99, 100 };
            CountingSort countingSort = new CountingSort();

            countingSort.Sort(arr);
            Assert.Equal(resultArr, arr);
        }
Exemplo n.º 20
0
        public void NormalArray_CountingSort()
        {
            var countingSort = new CountingSort();
            var numbers      = new[] { 2, 3, 1, 10, 5, 4, 6, 8, 7, 9 };
            var expected     = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            var result       = countingSort.Sort(numbers);

            Assert.AreEqual(expected, result);
        }
Exemplo n.º 21
0
        public void CountingSort_Smoke_Test()
        {
            var result = CountingSort.Sort(TestArray);

            for (int i = 0; i < TestArray.Length; i++)
            {
                Assert.AreEqual(i, result[i]);
            }
        }
Exemplo n.º 22
0
        public static void Sort(int[] data, int maxValue)
        {
            int p = 1;

            while (p <= maxValue)
            {
                CountingSort.Sort(data, 10, n => (n / p) % 10);
                p *= 10;
            }
        }
Exemplo n.º 23
0
        public void TestUnSortedArrayIsSortedCorrectly()
        {
            var tmp    = new CountingSort();
            var actual = new int[] { 1, 5, 2, 9, 6, 6, 4, 2 };

            tmp.Sort(actual, 9);
            var expected = new int[] { 1, 2, 2, 4, 5, 6, 6, 9 };

            Assert.That(actual, Is.EqualTo(expected));
        }
Exemplo n.º 24
0
        public void SortWithEmptyArray()
        {
            int[]        arr          = new int[] {};
            int[]        resultArr    = new int[] {};
            CountingSort countingSort = new CountingSort();

            countingSort.Sort(arr);

            Assert.Equal(resultArr, arr);
        }
Exemplo n.º 25
0
        private void CountingSortImplementation()
        {
            int len   = 8;
            int range = 10;

            int[] input  = new int[len];
            int[] output = new int[len];

            Console.WriteLine("Counting Sort: ");

            CountingSort s = new CountingSort();

            input = Utility.GetInputData(len, range);
            Utility.PrintAll("Input: ", input);
            output = s.Sort(input, range, SortOrderType.Asc);
            Utility.PrintAll("Output: ", input);
            output = s.Sort(input, range, SortOrderType.Desc);
            Utility.PrintAll("Output: ", input);
        }
        public void SortEmptyWrongMaxTest()
        {
            int[] data     = new int[] { };
            int[] expected = new int[] { };

            // Trying the test with a wrong max count
            var result = CountingSort.Sort(data, 20);

            CollectionAssert.AreEqual(expected, result, "CountingSort did not sort correctly");
        }
        public void SortNegativeMaxTest()
        {
            int[] data     = new int[] { -11, -10, 0, 2, 2 };
            int[] expected = new int[] { -11, -10, 0, 2, 2 };

            // Trying the test with a Negative max count
            var result = CountingSort.Sort(data, 2);

            CollectionAssert.AreEqual(expected, result, "CountingSort did not sort correctly");
        }
Exemplo n.º 28
0
    public void CountingSortTest()
    {
        var array    = "example".ToCharArray();
        var expected = "aeelmpx";
        int R        = 256;

        var sort = new CountingSort();

        sort.Sort(array, R);
        Assert.Equal(expected, new string(array));
    }
Exemplo n.º 29
0
        public void Ex2()
        {
            File_Lab10 file_Lab10 = new File_Lab10();

            CountingSort counting = new CountingSort(file_Lab10.RandomMoney());

            counting.Sort(0, 0);
            foreach (int i in counting.a)
            {
                Console.WriteLine(i + " ");
            }
        }
Exemplo n.º 30
0
        public void SampleTest1()
        {
            var lines = new[] { "AAA", "AAT", "ATA", "CAA", "GAA" };

            var sort = new CountingSort();

            sort.Sort(lines, 3, 0, lines.Length, new string[lines.Length]);

            var expected = new[] { "AAA", "CAA", "GAA", "ATA", "AAT" };

            CollectionAssert.AreEqual(expected, lines);
        }
Exemplo n.º 31
0
        public void BasicSort()
        {
            var items = new[] {1};
            var countingSort = new CountingSort(2);
            //countingSort.Sort(new int[]{});
            //countingSort.Sort(items);
            //Assert.AreEqual(1, items[0], "#Z01");

            countingSort = new CountingSort(7);
            items = new[] {5, 6, 6, 3, 2, 1, 3, 4};
            countingSort.Sort(items);
            Assert.IsTrue(items.IsSorted(), "#Z02");
        }