コード例 #1
0
            // Not Unity
#if !UNITY_5_3_OR_NEWER
            public static void Test()
            {
                // Sort two sorted array //
                int[] A = { 1, 5, 9, 100, 101, 102, 103 };
                int[] B = { 2, 3, 4, 11, 54, 86 };

                A.Println().PrintIsSorted_ascending();
                B.Println().PrintIsSorted_ascending();
                SortTwoSortedArray(A, B).Println().PrintIsSorted_ascending();


                const int dataLength = 10;
                const int dataMin    = -500;
                const int dataMax    = 500;

                int[] allRangeIntArray = MyArray.GenerateRandIntArray(dataLength);
                int[] inRangeIntArray  = MyArray.GenerateRandIntArray(dataLength, dataMin, dataMax);

                MyTest.SetTestSetting();

                /* Uncomment to see sorting flow
                 * many Print and so dataLength = 10 is suitable
                 * (add "*" here to uncomment quickly) -->*/

                "Start sorting flow / correctness test".lnPrintln();

                // BubbleSort //
                MyTest.TestExecutionTime(BubbleSort_Debug, allRangeIntArray).PrintIsSorted_ascending();
                MyTest.TestExecutionTime(BubbleSort_Debug, inRangeIntArray).PrintIsSorted_ascending();
                ln();
                // SelectionSort //
                MyTest.TestExecutionTime(SelectionSort_Debug, allRangeIntArray).PrintIsSorted_ascending();
                MyTest.TestExecutionTime(SelectionSort_Debug, inRangeIntArray).PrintIsSorted_ascending();
                ln();
                // CountingSort //
                MyTest.TestExecutionTime(CountingSort_Debug, inRangeIntArray, dataMin, dataMax).PrintIsSorted_ascending();
                MyTest.TestExecutionTime(CountingSort2, inRangeIntArray, dataMin, dataMax).PrintIsSorted_ascending();
                MyTest.TestExecutionTime(CountingSort2, inRangeIntArray, dataMin, dataMax);
                ln();
                // QuickSort //
                //MyTest.TestExecutionTime(QuickSort_Debug, allRangeIntArray).Print().PrintIsSorted_ascending();
                //MyTest.TestExecutionTime(QuickSort_Debug, inRangeIntArray).Print().PrintIsSorted_ascending();
                //ln();

                //*/

                // BubbleSort //
                MyTest.TestExecutionTime(BubbleSort, allRangeIntArray);
                MyTest.TestExecutionTime(BubbleSort, inRangeIntArray);
                // SelectionSort //
                MyTest.TestExecutionTime(SelectionSort, allRangeIntArray);
                MyTest.TestExecutionTime(SelectionSort, inRangeIntArray);
                // CountingSort //
                MyTest.TestExecutionTime(CountingSort, inRangeIntArray, dataMin, dataMax);
                MyTest.TestExecutionTime(CountingSort2, inRangeIntArray, dataMin, dataMax);

                // QuickSort //
                //MyTest.TestExecutionTime(QuickSort, allRangeIntArray);
                //MyTest.TestExecutionTime(QuickSort, inRangeIntArray);
            }
コード例 #2
0
        // Not Unity //
#if !UNITY_5_3_OR_NEWER
        public static T[] SelectionSort_Debug <T>(this T[] array) where T : IComparable
        {
            int ifCount = 0, matchIfCount = 0, swapCount = 0;

            T[] result = (T[])array.Clone();
            for (int i = 0; i < result.Length; i++)
            {
                int maxIndex = 0;
                for (int j = 0; j < result.Length - i; j++)
                {
                    MyArray.Println(result, Color.LightGreen, j, maxIndex);
                    ifCount++;
                    if (result[j].CompareTo(result[maxIndex]) > 0)
                    {
                        matchIfCount++;
                        MyArray.Println(result, Color.Yellow, j, maxIndex);
                        maxIndex = j;
                    }
                }
                Swap(ref result[maxIndex], ref result[result.Length - 1 - i]);
                MyArray.Println(result, Color.Red, maxIndex, result.Length - 1 - i);
                swapCount++;
            }
            Console.WriteLine("Check condition (Green) times: " + ifCount);
            Console.WriteLine("Match condition (Yellow) times: " + matchIfCount);
            Console.WriteLine("Swap (Red) times: " + swapCount);
            return(result);
        }
コード例 #3
0
        // Not Unity //
#if !UNITY_5_3_OR_NEWER
        public static T[] BubbleSort_Debug <T>(this T[] array) where T : IComparable
        {
            int ifCount = 0, matchIfCount = 0, swapCount = 0;

            T[] result = (T[])array.Clone();
            for (int i = 0; i < result.Length; i++)
            {
                for (int j = 1; j < result.Length - i; j++)
                {
                    MyArray.Println(result, Color.LightGreen, j - 1, j);
                    ifCount++;
                    if (result[j - 1].CompareTo(result[j]) > 0)
                    {
                        matchIfCount++;
                        MyArray.Println(result, Color.Yellow, j - 1, j);
                        Swap(ref result[j - 1], ref result[j]);
                        MyArray.Println(result, Color.Red, j - 1, j);
                        swapCount++;
                    }
                }
            }
            Console.WriteLine("Check condition (Green) times: " + ifCount);
            Console.WriteLine("Match condition (Yellow) times: " + matchIfCount);
            Console.WriteLine("Swap (Red) times: " + swapCount);
            return(result);
        }