Ejemplo n.º 1
0
        public void TestSortResult()
        {
            Sort = new BubbleSort(ListToSort);
            Sort.SortList();

            Assert.IsTrue(CompareList.SequenceEqual(Sort.SortedList));
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            int arraySize = 10;
            int arrayUpperLimits = 100;
            int repeatTimes = 1;
            bool enableLogging = true;

            int[] inputArray;
            double[] executionTime = new double[repeatTimes]; 
            Stopwatch stopwatch = new Stopwatch();

            //Slow - O(n^2)
            InsertionSort mInsertionSort = new InsertionSort();
            SelectionSort mSelectionSort = new SelectionSort();
            BubbleSort mBubbleSort = new BubbleSort();

            //Fast - O(n*lgn)
            MergeSort mMergeSort = new MergeSort();
            HeapSort mHeapSort = new HeapSort();
            QuickSort mQuickSort = new QuickSort();
           
            //Linear - O(n)
            CountingSort mCountingSort = new CountingSort();
            RadixSort mRadixSort = new RadixSort();
            BucketSort mBucketSort = new BucketSort();

            //Test the execution time repeatly.
            for (int i = 0; i < repeatTimes; i++)
            {
                //Always create new Array object, even though the contents are the same.
                //Because each array will be modified at the end of each loop.
                inputArray = ArrayHandler.Create(arraySize, enableLogging, arrayUpperLimits);
                //inputArray = ArrayHandler.CreateAlmostSorted(arraySize, enableLogging);
                //inputArray = new int[] { 122, 44, 122, 55, 33, 55, 44, 23};
                stopwatch.Start();
                //----------------------------------------------------------------------------------------//
                //             A L G O R I T H M         T E S T E D        H E R E                       //
                //----------------------------------------------------------------------------------------//

                //------- 0.Sort in VC# -------
                //Array.Sort(inputArray);                                                         //7ms 10^5

                //#####################################  O(n*n)  #############################################

                //------- 1.Insertion Sort ~ O(n^2) -------
                mInsertionSort.Sort(inputArray);                                                //95ms 10^4
                //mInsertionSort.SortWithTrace(inputArray);
                //mInsertionSort.Sort_Recursive(inputArray);

                //------- 2.Selection Sort ~ O(n^2) -------
                //mSelectionSort.Sort(inputArray);                                                //164ms 10^4
                //mSelectionSort.SortWithTrace(inputArray);

                //------- 3.Bubble Sort ~ O(n^2) -------
                //mBubbleSort.Sort(inputArray);                                                   //600ms 10^4
                //mBubbleSort.OriginalBubbleSort(inputArray);                                     //550ms 10^4

                //###################################  O(n*lgn)  #############################################

                //------- 4.Merge Sort ~ O(n*lgn) -------
                //mMergeSort.Sort(inputArray);                                                    //27ms 10^5
                //mMergeSort.Sort_Enhanced(inputArray);                                           //25ms 10^5

                //------- 5.Heap Sort ~ O(n*lgn) -------
                //mHeapSort.Sort(inputArray);                                                     //53ms 10^5

                //------- 6.Quick Sort ~ O(n*lgn) -------
                //mQuickSort.Sort(inputArray);                                                      //40ms 10^5
                //mQuickSort.Sort_Hoare(inputArray, enableLogging);                               //23ms 10^5
                //mQuickSort.Sort_Lomuto(inputArray, enableLogging);

                //######################################  O(n)  ##############################################

                //------- 7.Counting Sort ~ O(n) -------
                //inputArray = mCountingSort.Sort(inputArray);                                    //2ms 10^5

                //------- 8.Radix Sort ~ O(n) -------
                //inputArray = mRadixSort.Sort(inputArray, enableLogging);                        //114ms 10^5

                //------- 9.Bucket Sort ~ O(n) -------
                //inputArray = mBucketSort.Sort(inputArray);                                      //13ms 10^5

                //------------------------------------------------------------------------------------------
                //             A L G O R I T H M         T E S T        E N D E D
                //------------------------------------------------------------------------------------------
                stopwatch.Stop();
                executionTime[i] = stopwatch.ElapsedMilliseconds;
                Console.Write(executionTime[i] + " ");
                Console.WriteLine("");
                stopwatch.Reset();
                ArrayHandler.Print(inputArray, enableLogging);
            }

            //Print Execution Time
            double ts = executionTime.Average();
            Console.WriteLine("Average Execution Time in milliseconds: " + ts);

            Console.ReadLine();
        }