Exemplo n.º 1
0
        /// <summary>
        /// Show header, initialize virtualMemory, bubble sort poems by incremented segments and log number of exchanges used by each sort
        /// </summary>
        public void BubbleSortAndLog()
        {
            // Shows a header in the console similar to the comment at the top of this file
            Common_Code.ShowHeader();

            // Initializes all virtualMemory locations to -99
            Common_Code.VirtualMemoryInit();

            // Reads all three of the poems (TCOTLB.txt, RC.txt, GEAH.txt) in textDir (C:\devel\TFiles\)
            Week_2_Class.PoemReader();

            // Search virtualMemory for the text of the poems and put it in an array
            int[] allPoems = GetPoemsInVirtualMemory();

            int totalPoemsLength = allPoems.Length;

            // Variable to track the number of exchanges used in a sort
            int exchanges;

            // Sort a segment of allPoems of size 10
            exchanges = BubbleSortSegment(allPoems, 10);

            // Write the log from the size 10 sort
            WriteEfficiencyLog(10, exchanges, Common_Code.bubbleSort);

            // Sort and log segments of the array starting at 100 and incrementing by 100 every time (100, 200, 300, etc) up to 2000
            for (int i = 100; i <= 2000; i += 100)
            {
                exchanges = BubbleSortSegment(allPoems, i);
                WriteEfficiencyLog(i, exchanges, Common_Code.bubbleSort);
            }

            bool keepSorting = Common_Code.YesNo("Keep sorting segments from size 2000 to 5927 (may take a few minutes)");

            if (keepSorting)
            {
                // Sort and log segments of the array starting at 2000 and incrementing by 100 every time (2000, 2100, 2200, etc) up to 5900
                for (int i = 2000; i <= totalPoemsLength; i += 100)
                {
                    exchanges = BubbleSortSegment(allPoems, i);
                    WriteEfficiencyLog(i, exchanges, Common_Code.bubbleSort);
                }

                // Sort the entire array
                exchanges = BubbleSort(allPoems);

                // Write the log for the full sort
                WriteEfficiencyLog(totalPoemsLength, exchanges, Common_Code.bubbleSort);
            }

            // Write all locations in the sortable source array to log file for debugging
            Common_Code.IntArrayLog(allPoems, "AllPoems");

            // Write all locations in virtual memory to log file for debugging
            Common_Code.VirtualMemoryLog(10, true);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Sorts the values of the array by stepping through it repeatedly and comparing each value to the next.
        /// Returns the total number of exchanges made by the sort.
        /// </summary>
        /// <param name="toSort">The array to be sorted.</param>
        /// <returns>Total number of exchanges performed by the sort.</returns>
        public int BubbleSort(int[] toSort, bool log = true)
        {
            int totalExchanges = 0;

            for (int number = toSort.Length - 1; number > 0; number--)
            {
                for (int i = 0; i < number; i++)
                {
                    if ((toSort[i]) > (toSort[i + 1]))
                    {
                        ExchangeValues(toSort, i, (i + 1));
                        totalExchanges += 3;
                    }
                }
            }

            if (log)
            {
                // Write all locations in the sorted array to log for debugging
                Common_Code.IntArrayLog(toSort, "Size" + toSort.Length.ToString());
            }

            return(totalExchanges);
        }