Esempio n. 1
0
        public static void ThreadsSortDemo()
        {
            Console.Clear();
            Console.WriteLine("Task 4.3. Sorting Unit." + Environment.NewLine);
            Console.WriteLine("Sorting Unit consists of three parts:");
            Console.WriteLine("1. The sorting method from Task 4.1.");
            Console.WriteLine("2. A method, running the sorting method in an additional Thread.");
            Console.WriteLine("3. Event signalizing that sorting is finished." + Environment.NewLine);
            Console.WriteLine("Press enter to start a demonstration.");
            Console.ReadLine();

            var numberOfElements = 10000000;

            Console.WriteLine($"Let's create two arrays of {numberOfElements} elements and fill them with random numbers." +
                              "The first one is of ints, the second one is of doubles.");

            var sortUnit = new CustomSort();

            sortUnit.OnSortIsFinished += OnSortIsFinishedEventHandler;

            var arr1 = new int[numberOfElements];
            var arr2 = new double[numberOfElements];
            var rand = new Random();

            for (int i = 0; i < numberOfElements; i++)
            {
                arr1[i] = rand.Next(-numberOfElements, numberOfElements);
                arr2[i] = rand.NextDouble() * numberOfElements * 2 - numberOfElements;
            }

            Console.WriteLine($"arr1: {arr1}; arr1.Length = {arr1.Length}");
            Console.WriteLine($"arr2: {arr2}; arr2.Length = {arr2.Length}");

            Console.WriteLine(Environment.NewLine + "Sort parameter: arr1 - ascending order, arr2 - descending order.");

            sortUnit.SortInAdditionalThread <int>(arr1, (a, b) => a < b);
            sortUnit.SortInAdditionalThread <double>(arr2, (a, b) => a > b);

            Console.WriteLine("While the sorting is being performed in additional threads, " +
                              "we are seeing some activity in the main thread.");

            while (_threadsNumber != 2)
            {
                Console.Write("\rSorting");
                Thread.Sleep(500);
                if (_threadsNumber != 2)
                {
                    Console.Write("\r       ");
                    Thread.Sleep(500);
                }
            }
            _threadsNumber = 0;

            sortUnit.OnSortIsFinished -= OnSortIsFinishedEventHandler;

            Console.WriteLine("Press enter to show first and last 50 elements of sorted arrays:");
            Console.ReadLine();
            Console.WriteLine("arr1 first 50:");
            for (int i = 0; i < 50; i++)
            {
                Console.Write($"{arr1[i]}; ");
            }
            Console.WriteLine(Environment.NewLine + Environment.NewLine + "arr1 last 50:");
            for (int i = arr1.Length - 50; i < arr1.Length; i++)
            {
                Console.Write($"{arr1[i]}; ");
            }

            Console.WriteLine(Environment.NewLine + Environment.NewLine + "arr2 first 50:");
            for (int i = 0; i < 50; i++)
            {
                Console.Write($"{arr2[i]:N2}; ");
            }
            Console.WriteLine(Environment.NewLine + Environment.NewLine + "arr2 last 50:");
            for (int i = arr2.Length - 50; i < arr2.Length; i++)
            {
                Console.Write($"{arr2[i]:N2}; ");
            }

            Console.WriteLine(Environment.NewLine + Environment.NewLine +
                              "Press enter to exit");

            Console.ReadLine();
        }
Esempio n. 2
0
        public static void DoubleIntSortDemo()
        {
            var sortUnit    = new CustomSort();
            var rand        = new Random();
            var arrayLength = 20;

            Console.Clear();
            Console.WriteLine("Task 4.1. Custom Sort.");

            Console.WriteLine("You can sort array of ints and doubles, in ascending and descending order using one method.");
            Thread.Sleep(1000);
            Console.WriteLine("Let's start with the following array of doubles." + Environment.NewLine);
            var arrayDouble = new double[arrayLength];

            for (int i = 0; i < arrayLength; i++)
            {
                arrayDouble[i] = rand.NextDouble() * 2 * arrayLength - arrayLength;
            }
            CSharpLanguage.Display1DArray <double>(arrayDouble);

            Console.WriteLine();
            Thread.Sleep(2000);

            Console.WriteLine(Environment.NewLine + "It can be sorted in ascending order by calling the following method:");
            Console.WriteLine("SortArray <double> ( arrayDouble, (a,b) => a < b );");
            Console.WriteLine("Press enter to sort.");
            Console.ReadLine();
            sortUnit.SortArray <double>(arrayDouble, (a, b) => a < b);
            CSharpLanguage.Display1DArray <double>(arrayDouble);

            Console.WriteLine();
            Console.WriteLine(Environment.NewLine + "It can be sorted in descending order by calling the following method:");
            Console.WriteLine("SortArray <double> ( arrayDouble, (a,b) => a > b );");
            Console.WriteLine("Press enter to sort.");
            Console.ReadLine();
            sortUnit.SortArray <double>(arrayDouble, (a, b) => a > b);
            CSharpLanguage.Display1DArray <double>(arrayDouble);

            Console.WriteLine();

            Console.WriteLine(Environment.NewLine + "Press enter to perform these operations with an array of ints.");
            Console.ReadLine();

            Console.Clear();
            Console.WriteLine("Array of ints:" + Environment.NewLine);

            var arrayInt = new int[arrayLength];

            for (int i = 0; i < arrayLength; i++)
            {
                arrayInt[i] = rand.Next(-arrayLength, arrayLength + 1);
            }
            CSharpLanguage.Display1DArray <int>(arrayInt);
            Console.WriteLine();

            Console.WriteLine(Environment.NewLine + "Press enter to sort ascending.");
            Console.WriteLine("SortArray <int> ( arrayInt, (a,b) => a < b );");
            Console.ReadLine();
            sortUnit.SortArray <int>(arrayInt, (a, b) => a < b);

            CSharpLanguage.Display1DArray <int>(arrayInt);
            Console.WriteLine();

            Console.WriteLine(Environment.NewLine + "Press enter to sort descending.");
            Console.WriteLine("SortArray <int> ( arrayInt, (a,b) => a > b );");
            Console.ReadLine();
            sortUnit.SortArray <int>(arrayInt, (a, b) => a > b);

            CSharpLanguage.Display1DArray <int>(arrayInt);

            Console.WriteLine(Environment.NewLine);
            Console.WriteLine("The end. Press enter to exit.");
            Console.ReadLine();
        }