Ejemplo n.º 1
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();
        }
Ejemplo n.º 2
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 ----------");
        }
Ejemplo n.º 3
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();
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            const int TANTI = 20;
            Random    r     = new Random();

            int[]      array  = new int[TANTI];
            List <int> numeri = new List <int>();

            for (int i = 0; i < TANTI; i++)
            {
                int k;
                do
                {
                    k = r.Next(0, 100);
                } while (numeri.Contains(k));
                numeri.Add(k);
                array[i] = k;
            }
            Console.WriteLine("STAMPO ARRAY NON ORDINATO: ");
            for (int i = 0; i < array.Length; i++)
            {
                Console.Write(array[i] + " ");
            }
            Console.WriteLine();
            int[] sortedArray = CountingSort.Counting_Sort(array);
            Console.WriteLine("STAMPO ARRAY ORDINATO: ");
            for (int i = 0; i < TANTI; i++)
            {
                Console.Write(sortedArray[i] + " ");
            }
        }
        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 }));
        }
    /// <summary>
    /// sortiraj od najveceg do najmanjeg
    /// </summary>
    public void SortToLowest()
    {
        if (listItemChangeable.Count != 0 && listItemChangeable != null)
        {
            DeleteSlots();

            #region sortiranje itema za upis u inventory
            Item[] sortedItems = new Item[listItemChangeable.Count];
            for (int i = 0; i < listItemChangeable.Count; i++)
            {
                sortedItems[i] = listItemChangeable[i];
            }
            CountingSort countSort = new CountingSort();
            sortedItems = countSort.SortToLow(sortedItems);

            for (int i = 0; i < sortedItems.Length; i++)
            {
                listItemChangeable[i] = sortedItems[i];
            }
            //listItemChangeable.Reverse();
            #endregion

            AddToInventory(listItemChangeable);
        }
    }
Ejemplo n.º 7
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");
        }
Ejemplo n.º 8
0
        static void Main(string[] args)
        {
            Console.Write("Введiть масив -> ");
            var oldArr = Console.ReadLine().Trim().Split(new char[] { ' ' }).Select(x => Convert.ToInt32(x)).ToArray();

            while (true)
            {
                Console.Write("1. Пiдрахунком\n2. За розрядами\n-> ");
                switch (Int32.Parse(Console.ReadLine()))
                {
                case 1:
                    var arr = CountingSort.Run(oldArr);
                    foreach (var item in arr)
                    {
                        Console.Write(item + " ");
                    }
                    Console.ReadLine();
                    break;

                case 2:
                    var arrRadix = RadixSort.Run(oldArr);
                    foreach (var item in arrRadix)
                    {
                        Console.Write(item + " ");
                    }
                    Console.ReadLine();
                    break;
                }
            }
        }
Ejemplo n.º 9
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]);
            }
        }
Ejemplo n.º 10
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]));
                    }
                }
            }
        }
        private void btn123(object sender, EventArgs e)
        {
            switch ((sender as Button).Text)
            {
            case "Quick Sort Algorithm Simulation":
                QuickSort frmquick = new QuickSort();
                GlobalClass.CheckMdiChildren(frmquick);
                break;

            case "Counting Sort Algorithm Simulation":
                CountingSort frmcount = new CountingSort();
                GlobalClass.CheckMdiChildren(frmcount);
                break;

            case "Radix Sort Algorithm Simulation":
                RadixSort frmradix = new RadixSort();
                GlobalClass.CheckMdiChildren(frmradix);
                break;

            case "Dice Throw Problem":
                DiceThrow frmdicethrow = new DiceThrow();
                GlobalClass.CheckMdiChildren(frmdicethrow);
                break;

            case "Travelling Salesman Problem":
                TSP frmTSP = new TSP();
                GlobalClass.CheckMdiChildren(frmTSP);
                break;
            }
        }
Ejemplo n.º 12
0
        public CountingSortTests()
        {
            var sort = new CountingSort <int>();

            func      = array => sort.Sort(array);
            this.sort = sort;
            algorithm = nameof(CountingSort <int>);
        }
Ejemplo n.º 13
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);
        }
Ejemplo n.º 14
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);
        }
Ejemplo n.º 15
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);
        }
        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");
        }
        public void SortArrayInAscendingOrderWintCountingSortTest()
        {
            var numbers       = new[] { 2, 5, 3, 0, 2, 3, 0, 3 };
            var countingSort  = new CountingSort();
            var sortedNumbers = countingSort.Run(numbers);

            Assert.IsTrue(sortedNumbers.IsSortedAscending());
        }
Ejemplo n.º 18
0
        public void CountingSortTest()
        {
            // Arrange
            var input = new int[] { 0, 5, 5, 7, 2, 1, 3, 8, 9, 1, 10 };

            // Act & Assert
            CountingSort.CountingSortAlgo(input, 10).Should().BeInAscendingOrder();
        }
        /// <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");
        }
        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");
        }
        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]);
            }
        }
Ejemplo n.º 23
0
 public void CountingSort100()
 {
     var input = new[] {51, 97, 66, 47, 47, 76, 83, 5};
     var expected = new[] {5, 47, 47, 51, 66, 76, 83, 97};
     
     CountingSort.SortAscending(input, 0, 100);
     
     CollectionAssert.AreEqual(expected, input);
 }
Ejemplo n.º 24
0
        public void CountingSort_Smoke_Test()
        {
            var result = CountingSort.Sort(TestArray);

            for (int i = 0; i < TestArray.Length; i++)
            {
                Assert.AreEqual(i, result[i]);
            }
        }
Ejemplo n.º 25
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);
        }
Ejemplo n.º 26
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);
        }
Ejemplo n.º 27
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);
        }
Ejemplo n.º 28
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));
        }
        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");
        }
Ejemplo 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");
        }