Esempio n. 1
0
        static void Main(string[] args)
        {
            //Added 3/4/2020 td
            //
            //int[] array_integers = new int[] { 56, 23, 90 };
            //int[] sub_array = new int[2];
            //array_integers.CopyTo(sub_array, 0);

            var objSorting_BubbleSort = new ComputerScience_Sorting_Lib.SortingByBubbleSort();
            var objSorting_Selection  = new ComputerScience_Sorting_Lib.SortingBySelectionSort();
            var objSorting_MergeSort  = new ComputerScience_Sorting_Lib.SortingByMergeSort_TD();

            //Console.WriteLine("Hello World!");
            //string strListOfNumbersToSort = "";
            List <int> list_of_nums = new List <int>();
            //var tupleOfNums = new Tuple<List<int>, string>() { list_of_nums, "" };
            Tuple <List <int>, string> tupleOfNums = null;

            Console.WriteLine("__");
            Console.WriteLine("_____________________________________________");
            Console.WriteLine("____                                  _______");
            Console.WriteLine("____    Sorting Arrays of Integers    _______");
            Console.WriteLine("____                                  _______");
            Console.WriteLine("_____________________________________________");
            Console.WriteLine("__");
            Console.WriteLine("__Generate integer array via random-number functionality? (Y/N):");

            bool boolUseRandom = (Console.ReadLine().ToUpper().StartsWith("Y"));

            if (boolUseRandom)
            {
                tupleOfNums = CreateRandomListOfIntegers();
            }
            else
            {
                Console.WriteLine("__");
                Console.WriteLine("__Use a pre-defined list of 100 integers? (Y/N):");

                bool boolUsePredefined = (Console.ReadLine().ToUpper().StartsWith("Y"));
                if (boolUsePredefined)
                {
                    //var objSorting_BubbleSort = new ComputerScience_Sorting_Lib.SortingByBubbleSort();
                    tupleOfNums = objSorting_BubbleSort.DataSetToTest_Tuple(Enum_DataSetSize.N_is100);
                }
                else
                {
                    Console.WriteLine("__");
                    Console.WriteLine("__Compose your own list of 100 integers? (Y/N):");

                    bool boolUserDefinesHisList = (Console.ReadLine().ToUpper().StartsWith("Y"));
                    if (boolUserDefinesHisList)
                    {
                        tupleOfNums = UserDefinedListOfIntegers();
                    }
                    else
                    {
                        //Program.Main("Run program again");
                        //Program.Main(new string[] { "Run program again" });
                        Console.WriteLine("__");
                        Console.WriteLine("__Press the Enter key, to exit the program.");
                        Console.ReadLine();
                    }
                }
            }
            //Console.WriteLine("__");
            //Console.WriteLine("__Enter huge decimal number #2:");
            //listOfNumbers.Add(Console.ReadLine());

            //
            //Processing of the list.
            //
            if (tupleOfNums != null)
            {
                Console.WriteLine("__");
                Console.WriteLine("__Here is the list of numbers to be sorted:");
                Console.WriteLine("__");
                Console.WriteLine(tupleOfNums.Item2.ToString());
                Console.WriteLine("__");
                //int[] objSortedArray_Bubble = objSorting_BubbleSort.Sort_ReturnMilliseconds();
                int[] array_ofNums;

                array_ofNums = tupleOfNums.Item1.ToArray();  // Refresh / initialize.
                double time_millisecondsBubble = objSorting_BubbleSort.Sort_ReturnMilliseconds(array_ofNums);
                string strSortedList_Bubble    = array_ofNums.ToString();

                array_ofNums = tupleOfNums.Item1.ToArray(); // Refresh.
                double time_millisecondsSelect = objSorting_Selection.Sort_ReturnMilliseconds(array_ofNums);
                array_ofNums = tupleOfNums.Item1.ToArray(); // Refresh.
                double time_millisecondsMerge = objSorting_MergeSort.Sort_ReturnMilliseconds(array_ofNums);

                Console.WriteLine("__");
                Console.WriteLine("__Here are the list(s) of sorted numbers:");
                Console.WriteLine("__");
            }
        }
        private int[] Merge_Arrays(int[] param_array1, int[] param_array2)
        {
            //
            // Added 3/3/2020 thomas d.
            //
            bool boolEqualLength  = (param_array1.Length == param_array2.Length);
            bool bUnequalLength   = (param_array1.Length != param_array2.Length);
            bool bArray1_IsLonger = (param_array1.Length > param_array2.Length);

            int intLengthDifference = (bArray1_IsLonger ?
                                       (param_array1.Length - param_array2.Length) :
                                       (param_array2.Length - param_array1.Length));

            int intLengthCommon   = (param_array1.Length > param_array2.Length ? param_array2.Length : param_array1.Length);
            int intLengthCombined = (param_array1.Length + param_array2.Length);

            int[] array_out_merged = new int[intLengthCombined];

            if (intLengthDifference > 1)
            {
                throw new NotSupportedException("Arrays must be nearly-equal in length.");
            }

            //
            //Looping !!   Added 3/3/2020 thomas downes
            //
            int  intIndexOutput    = 0;
            int  intIndexInputArr1 = 0;
            int  intIndexInputArr2 = 0;
            int  intValueGreater;
            int  intValueLesser;
            bool bKeepLooping = True; //Added 3/3/2020 thomas downes

            //for (int intIndexInput = 0; intIndexInput < (-1 + intLengthCommon); intIndexInput++)
            //{
            //    intValueGreater = (param_array1[intIndexInput] > param_array2[intIndexInput] ? param_array1[intIndexInput] : param_array2[intIndexInput]);
            //    intValueLesser = (param_array1[intIndexInput] < param_array2[intIndexInput] ? param_array1[intIndexInput] : param_array2[intIndexInput]);

            //    array_out_merged[intIndexOutput] = intValueLesser;
            //    Sort_AdjacentPair(array_out_merged, intIndexOutput);
            //    intIndexOutput++;
            //    array_out_merged[intIndexOutput] = intValueGreater;
            //    Sort_AdjacentPair(array_out_merged, intIndexOutput);
            //    intIndexOutput++;
            //}

            do
            {
                //bool bExhaustedArray1 = (intIndexInputArr1 > -1 + param_array1.Length);
                //bool bExhaustedArray2 = (intIndexInputArr2 > -1 + param_array2.Length);
                bool bWithinArray1 = (intIndexInputArr1 <= -1 + param_array1.Length);
                bool bWithinArray2 = (intIndexInputArr2 <= -1 + param_array2.Length);
                bool bLesserIsArray1;

                //if ((intIndexInputArr1 <= -1 + param_array1.Length) && (param_array1[intIndexInputArr1] < param_array2[intIndexInputArr2]))
                if (bWithinArray1 && bWithinArray2)
                {
                    bLesserIsArray1 = (param_array1[intIndexInputArr1] < param_array2[intIndexInputArr2]);
                    intValueLesser  = (bLesserIsArray1 ? param_array1[intIndexInputArr1] : param_array1[intIndexInputArr2]);
                    if (bLesserIsArray1)
                    {
                        intIndexInputArr1++;                   // Move the index for Array #1.
                    }
                    if (False == bLesserIsArray1)
                    {
                        intIndexInputArr2++;                           // Move the index for Array #2.
                    }
                }
                else if (bWithinArray1)  // (intIndexInputArr1 <= -1 + param_array1.Length)
                {
                    intValueLesser = param_array1[intIndexInputArr1];
                    intIndexInputArr1++; // Move the index for Array #1.
                }
                else if (bWithinArray2)  // (intIndexInputArr2 <= -1 + param_array2.Length)
                {
                    intValueLesser = param_array2[intIndexInputArr2];
                    intIndexInputArr2++; // Move the index for Array #2.
                }
                else
                {
                    intValueLesser = -99;
                }

                array_out_merged[intIndexOutput] = intValueLesser;
                intIndexOutput++;
                bKeepLooping = (intIndexInputArr1 <= -1 + param_array1.Length) ||
                               (intIndexInputArr2 <= (-1 + param_array2.Length));
            } while (bKeepLooping);


            if (bArray1_IsLonger && intLengthDifference == 1)
            {
                //
                // Array # 1 is longer.
                //
                array_out_merged[intIndexOutput] = param_array1[-1 + param_array1.Length];
                //Sort_AdjacentPair(array_out_merged, intIndexOutput);
                SortingByMergeSort_TD.Sort_AdjacentPair(array_out_merged, intIndexOutput);
            }
            else if (intLengthDifference == 1)
            {
                //
                // Array # 2 is longer.
                //
                array_out_merged[intIndexOutput] = param_array2[-1 + param_array2.Length];
                //Sort_AdjacentPair(array_out_merged, intIndexOutput);
                SortingByMergeSort_TD.Sort_AdjacentPair(array_out_merged, intIndexOutput);
            }

            return(array_out_merged);
        }