Example #1
0
        /// <summary>
        /// 4. *Подсчитать количество операций для каждой из сортировок и сравнить его с асимптотической сложностью алгоритма.
        /// </summary>
        static void Task4()
        {
            SupportMethods.PrepareConsoleForHomeTask("4. *Подсчитать количество операций для каждой из сортировок и сравнить его с асимптотической сложностью алгоритма.\n");

            //int arrsize = SupportMethods.RequestIntValue("Please type size of array: ");
            //int min = SupportMethods.RequestIntValue("Please type min value of element: ");
            //int max = SupportMethods.RequestIntValue("Please type max value of element: ");

            int arrsize = 10000;
            int min     = 0;
            int max     = 10000;

            MyArray arrBS    = new MyArray(arrsize, min, max);
            MyArray arrBSO   = new MyArray(arrsize);
            MyArray arrBSSO  = new MyArray(arrsize);
            MyArray arrBSSOS = new MyArray(arrsize);
            MyArray arrSS    = new MyArray(arrsize);
            MyArray arrSSO   = new MyArray(arrsize);
            MyArray arrSSSO  = new MyArray(arrsize);
            MyArray arrSSSOS = new MyArray(arrsize);

            Array.Copy(arrBS.GetArray, arrBSO.GetArray, arrsize);
            Array.Copy(arrBS.GetArray, arrBSSO.GetArray, arrsize);
            Array.Copy(arrBS.GetArray, arrBSSOS.GetArray, arrsize);
            Array.Copy(arrBS.GetArray, arrSS.GetArray, arrsize);
            Array.Copy(arrBS.GetArray, arrSSO.GetArray, arrsize);
            Array.Copy(arrBS.GetArray, arrSSSO.GetArray, arrsize);
            Array.Copy(arrBS.GetArray, arrSSSOS.GetArray, arrsize);

            int asymptoticComplexityRounds = arrsize;
            int asymptoticComplexitySteps  = arrsize * arrsize;

            int rounds = 0;
            int steps  = 0;

            arrBS.BubbleSort(ref rounds, ref steps);
            Console.WriteLine($"Rounds Real: {rounds}, Asymptotic Complexity Rounds: {asymptoticComplexityRounds} | Steps Real: {steps}, Asymptotic Complexity Steps: {asymptoticComplexitySteps}");

            rounds = 0;
            steps  = 0;

            arrBSO.BubbleSortOptimized(ref rounds, ref steps);
            Console.WriteLine($"Rounds Real: {rounds}, Asymptotic Complexity Rounds: {asymptoticComplexityRounds} | Steps Real: {steps}, Asymptotic Complexity Steps: {asymptoticComplexitySteps}");

            rounds = 0;
            steps  = 0;

            arrBSSO.BubbleShakeSortOptimized(ref rounds, ref steps);
            Console.WriteLine($"Rounds Real: {rounds}, Asymptotic Complexity Rounds: {asymptoticComplexityRounds} | Steps Real: {steps}, Asymptotic Complexity Steps: {asymptoticComplexitySteps}");

            rounds = 0;
            steps  = 0;

            arrBSSOS.BubbleShakeSortOptimizedSecond(ref rounds, ref steps);
            Console.WriteLine($"Rounds Real: {rounds}, Asymptotic Complexity Rounds: {asymptoticComplexityRounds} | Steps Real: {steps}, Asymptotic Complexity Steps: {asymptoticComplexitySteps}");

            rounds = 0;
            steps  = 0;

            arrSS.ShakerSort(ref rounds, ref steps);
            Console.WriteLine($"Rounds Real: {rounds}, Asymptotic Complexity Rounds: {asymptoticComplexityRounds} | Steps Real: {steps}, Asymptotic Complexity Steps: {asymptoticComplexitySteps}");

            rounds = 0;
            steps  = 0;

            arrSSO.ShakerSortOptimized(ref rounds, ref steps);
            Console.WriteLine($"Rounds Real: {rounds}, Asymptotic Complexity Rounds: {asymptoticComplexityRounds} | Steps Real: {steps}, Asymptotic Complexity Steps: {asymptoticComplexitySteps}");

            rounds = 0;
            steps  = 0;

            arrSSSO.ShakerShakeSortOptimized(ref rounds, ref steps);
            Console.WriteLine($"Rounds Real: {rounds}, Asymptotic Complexity Rounds: {asymptoticComplexityRounds} | Steps Real: {steps}, Asymptotic Complexity Steps: {asymptoticComplexitySteps}");

            rounds = 0;
            steps  = 0;

            arrSSSOS.ShakerShakeSortOptimizedSecond(ref rounds, ref steps);
            Console.WriteLine($"Rounds Real: {rounds}, Asymptotic Complexity Rounds: {asymptoticComplexityRounds} | Steps Real: {steps}, Asymptotic Complexity Steps: {asymptoticComplexitySteps}");

            SupportMethods.Pause();
        }
Example #2
0
        /// <summary>
        /// 2. *Реализовать шейкерную сортировку.
        /// </summary>
        static void Task2()
        {
            SupportMethods.PrepareConsoleForHomeTask("2. *Реализовать шейкерную сортировку.\n");

            //int arrsize = SupportMethods.RequestIntValue("Please type size of array: ");
            //int min = SupportMethods.RequestIntValue("Please type min value of element: ");
            //int max = SupportMethods.RequestIntValue("Please type max value of element: ");

            int arrsize = 10000;
            int min     = 0;
            int max     = 10000;

            MyArray arrSS    = new MyArray(arrsize, min, max);
            MyArray arrSSO   = new MyArray(arrsize);
            MyArray arrSSSO  = new MyArray(arrsize);
            MyArray arrSSSOS = new MyArray(arrsize);

            Array.Copy(arrSS.GetArray, arrSSO.GetArray, arrsize);
            Array.Copy(arrSS.GetArray, arrSSSO.GetArray, arrsize);
            Array.Copy(arrSS.GetArray, arrSSSOS.GetArray, arrsize);

            // Print out array
            // SupportMethods.Pause($"Array:\n{arrSS.ToString()}\nPress any key to continue...\n");

            // ********************************
            // Shaker Sort
            int rounds = 0;
            int steps  = 0;

            Console.WriteLine("Shaker Sort");
            DateTime dtStart = DateTime.Now;

            Console.WriteLine($"Started at: {dtStart}");

            arrSS.ShakerSort(ref rounds, ref steps);

            DateTime dtEnd = DateTime.Now;

            Console.WriteLine($"Finished at: {dtEnd}");

            // Print out result
            //SupportMethods.Pause($"Sorted array using Bubble Sort:\n{arrSS.ToString()}\nSorted in {dtEnd - dtStart} at {rounds} rounds with {steps} steps\nPress any key to continue...\n");
            SupportMethods.Pause($"Sorted: {arrSS.isSorted}, in {dtEnd - dtStart} at {rounds} rounds with {steps} steps\nPress any key to continue...\n");

            // ********************************
            // Shaker Sort Optimized using flag
            rounds = 0;
            steps  = 0;

            Console.WriteLine("Shaker Sort Optimized using flag");
            dtStart = DateTime.Now;
            Console.WriteLine($"Started at: {dtStart}");

            arrSSO.ShakerSortOptimized(ref rounds, ref steps);

            dtEnd = DateTime.Now;
            Console.WriteLine($"Finished at: {dtEnd}");

            // Print out result
            //SupportMethods.Pause($"Sorted array using Bubble Sort:\n{arrSSO.ToString()}\nSorted in {dtEnd - dtStart} at {rounds} rounds with {steps} steps\nPress any key to continue...\n");
            SupportMethods.Pause($"Sorted: {arrSSO.isSorted}, in {dtEnd - dtStart} at {rounds} rounds with {steps} steps\nPress any key to continue...\n");

            // ********************************
            // Shaker Sort Optimized using false Shaker sort and flag
            rounds = 0;
            steps  = 0;

            Console.WriteLine("Shaker Sort Optimized using false Shaker sort and flag");
            dtStart = DateTime.Now;
            Console.WriteLine($"Started at: {dtStart}");

            arrSSSO.ShakerShakeSortOptimized(ref rounds, ref steps);

            dtEnd = DateTime.Now;
            Console.WriteLine($"Finished at: {dtEnd}");

            // Print out result
            //SupportMethods.Pause($"Sorted array using Bubble Sort:\n{arrSSSO.ToString()}\nSorted in {dtEnd - dtStart} at {rounds} rounds with {steps} steps\nPress any key to continue...\n");
            SupportMethods.Pause($"Sorted: {arrSSSO.isSorted}, in {dtEnd - dtStart} at {rounds} rounds with {steps} steps\nPress any key to continue...\n");

            // ********************************
            // Shaker Sort Optimized using false Shaker sort, flag and optimization size of array
            rounds = 0;
            steps  = 0;

            Console.WriteLine("Shaker Sort Optimized using false Shaker sort, flag and optimization size of array");
            dtStart = DateTime.Now;
            Console.WriteLine($"Started at: {dtStart}");

            arrSSSOS.ShakerShakeSortOptimizedSecond(ref rounds, ref steps);

            dtEnd = DateTime.Now;
            Console.WriteLine($"Finished at: {dtEnd}");

            // Print out result
            //SupportMethods.Pause($"Sorted array using Bubble Sort:\n{arrSSSOS.ToString()}\nSorted in {dtEnd - dtStart} at {rounds} rounds with {steps} steps\nPress any key to continue...\n");
            SupportMethods.Pause($"Sorted: {arrSSSOS.isSorted}, in {dtEnd - dtStart} at {rounds} rounds with {steps} steps\nPress any key to continue...\n");
        }