Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            // -------------------- Bubble Sort --------------------
            var bubbleNumbers = new[] { 7, 3, 1, 4, 6, 2, 3 };
            var bubbleSorter  = new BubbleSort();

            bubbleSorter.Sort(bubbleNumbers);
            Console.WriteLine($"[{string.Join(", ", bubbleNumbers)}]");

            // -------------------- Selection Sort --------------------
            var selectionNumbers = new[] { 7, 3, 1, 4, 6, 2, 3 };
            var selectionSorter  = new SelectionSort();

            selectionSorter.Sort(selectionNumbers);
            Console.WriteLine("[{0}]", string.Join(", ", selectionNumbers));

            // -------------------- Insertion Sort --------------------
            var insertionNumbers = new[] { 7, 3, 1, 4, 6, 2, 3 };
            var insertionSorter  = new InsertionSort();

            insertionSorter.Sort(insertionNumbers);
            Console.WriteLine("[{0}]", string.Join(", ", insertionNumbers));

            // -------------------- Merge Sort --------------------
            int[] mergeNumbers = { 7, 3, 1, 4, 6, 2, 3 };
            var   mergeSorter  = new MergeSort();

            mergeSorter.Sort(mergeNumbers);
            Console.WriteLine("[{0}]", string.Join(", ", mergeNumbers));

            // -------------------- Quick Sort --------------------
            int[] quickNumbers = { 7, 3, 1, 4, 6, 2, 3 };
            var   quickSorter  = new QuickSort();

            quickSorter.Sort(quickNumbers);
            Console.WriteLine("[{0}]", string.Join(", ", quickNumbers));

            // -------------------- Counting Sort --------------------
            int[] countingNumbers = { 7, 3, 1, 4, 6, 2, 3 };
            var   countingSorter  = new CountingSort();

            countingSorter.Sort(countingNumbers);
            Console.WriteLine("[{0}]", string.Join(", ", countingNumbers));

            // -------------------- Bucket Sort --------------------
            int[] bucketNumbers = { 7, 3, 1, 4, 6, 2, 3 };
            var   bucketSorter  = new BucketSort();

            bucketSorter.Sort(bucketNumbers, 3);
            Console.WriteLine("[{0}]", string.Join(", ", bucketNumbers));
        }
Ejemplo n.º 2
0
        static void QuicksortTest()
        {
            int[] integerValues = { -11, 12, -42, 0, 1, 90, 68, 6, -9 };
            QuickSort.Sort(integerValues);
            Console.WriteLine(string.Join(" | ", integerValues));

            float[] floatValues = { -11.2f, 12.56f, -42.59f, 0.0f, 1.1f, 90.9f, 68.68f, 6.1f, -9.8f };
            QuickSort.Sort(floatValues);
            Console.WriteLine(string.Join(" | ", floatValues));

            string[] stringValues = { "Mary", "Marcin", "Ann", "James", "George", "Nicole" };
            QuickSort.Sort(stringValues);
            Console.WriteLine(string.Join(" | ", stringValues));
        }
Ejemplo n.º 3
0
        private static SortResult SortArrayWithTimeResult(string identifier, int[] array, bool useMedianOfThree, int insertionSortSize)
        {
            var quickSort   = new QuickSort(useMedianOfThree, insertionSortSize);
            var startTime   = DateTime.Now;
            var sortedArray = quickSort.Sort(array);

            if (insertionSortSize > 0)
            {
                quickSort.InsertionSort(sortedArray, 0, sortedArray.Length - 1);
            }
            var stopTime = DateTime.Now;

            var totalMilliseconds = (stopTime - startTime).TotalMilliseconds;

            return(new SortResult(identifier, totalMilliseconds, sortedArray));
        }
Ejemplo n.º 4
0
        public static void Main(string[] args)
        {
            // Bubble sort.
            Console.WriteLine("Bubble sort");
            int[]      arr        = new int[] { 5, 4, 3, 2, 1 };
            BubbleSort bubbleSort = new BubbleSort(arr);

            bubbleSort.Sort();
            Console.WriteLine(bubbleSort.ToString());

            //Insertion sort
            Console.WriteLine("Insertion sort");
            InsertionSort insertionSort = new InsertionSort(new int[] { 5, 4, 3, 2, 1 });

            insertionSort.Sort();
            Console.WriteLine(insertionSort.ToString());


            //Selection sort
            Console.WriteLine("Selection sort");
            SelectionSort selectionSort = new SelectionSort(new int[] { 5, 4, 3, 2, 1 });

            selectionSort.Sort();
            Console.WriteLine(selectionSort.ToString());

            // Quicksort
            Console.WriteLine("Quick sort");
            QuickSort quickSort = new QuickSort(new int[] { 5, 4, 3, 2, 1 });

            quickSort.Sort();
            Console.WriteLine(quickSort.ToString());

            // Merge sort
            Console.WriteLine("Merge sort");
            MergeSortImpl mergeSortImpl = new MergeSortImpl(new int[] { 5, 4, 3, 2, 1 });

            mergeSortImpl.Sort();
            Console.WriteLine(mergeSortImpl.ToString());

            // Heap sort
            Console.WriteLine("Heap sort");
            HeapSortImpl heapSortImpl = new HeapSortImpl(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });

            heapSortImpl.Sort();
            Console.WriteLine(heapSortImpl.ToString());
            Console.Read();
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            var rnd     = new Random();
            var arrSize = rnd.Next(1, 1000);

            int[] arr = Enumerable.Repeat(0, arrSize).Select(x => rnd.Next(int.MinValue, int.MaxValue)).ToArray();
            var   bs  = (int[])arr.Clone();

            BubbleSort.Sort(bs);
            Assert.IsTrue(IsSorted(bs));

            var ss = (int[])arr.Clone();

            SelectionSort.Sort(ss);
            Assert.IsTrue(IsSorted(ss));

            var @is = (int[])arr.Clone();

            InsertionSort.Sort(@is);
            Assert.IsTrue(IsSorted(@is));

            var ms = (int[])arr.Clone();

            MergeSort.Sort(ms);
            Assert.IsTrue(IsSorted(ms));

            var qs = (int[])arr.Clone();

            QuickSort.Sort(qs);
            Assert.IsTrue(IsSorted(qs));

            var cs = (int[])arr.Clone();

            CountingSort.Sort(cs);
            Assert.IsTrue(IsSorted(cs));

            var rs = (int[])arr.Clone();

            RadixSort.Sort(rs);
            Assert.IsTrue(IsSorted(rs));

            var hs = (int[])arr.Clone();

            HeapSort.Sort(hs);
            Assert.IsTrue(IsSorted(hs));
        }
        static void Main(string[] args)
        {
            // Arrays to sort
            int[]    integerValues = { -11, 12, -42, 0, 1, 90, 68, 6, -9 };
            string[] stringValues  = { "Mary", "Marcin", "Ann", "James", "George", "Nicole" };

            // Apply methods here
            SelectionSort.Sort(integerValues);
            SelectionSort.Sort(stringValues);
            InsertionSort.Sort(integerValues);
            BubbleSort.Sort(integerValues);
            QuickSort.Sort(integerValues);

            // Read output here
            Console.WriteLine(string.Join(" | ", integerValues));
            Console.WriteLine(string.Join(" | ", stringValues));
            Console.ReadLine();
        }
Ejemplo n.º 7
0
        static void RunSorts()
        {
            var watch = new System.Diagnostics.Stopwatch();

            Console.WriteLine("Unsorted int array size of {0}", unsortedArray.Length);

            watch.Start();
            InsertionSort ISort = new InsertionSort(unsortedArray);

            watch.Stop();

            Console.WriteLine("Insertion Sort - Ticks/ Time in Milliseconds spent to create object and sort the array: " + watch.Elapsed.Ticks);
            watch.Reset();

            watch.Start();
            MergeSort MSort = new MergeSort(unsortedArray);

            watch.Stop();

            Console.WriteLine("Merge Sort - Ticks/ Time in Milliseconds spent to create object and sort the array: " + watch.Elapsed.Ticks);

            watch.Reset();

            watch.Start();
            QuickSort QSort = new QuickSort(unsortedArray);

            watch.Stop();

            Console.WriteLine("Quick Sort - Ticks/ Time in Milliseconds spent to create object and sort the array: " + watch.Elapsed.Ticks);



            watch.Reset();
            watch.Start();
            Array.Sort(unsortedArray);
            watch.Stop();

            //This method uses the Array.Sort method, which applies the introspective sort as follows:
            //•If the partition size is fewer than 16 elements, it uses an insertion sort algorithm.
            //•If the number of partitions exceeds 2 * LogN, where N is the range of the input array, it uses a Heapsort algorithm.
            //•Otherwise, it uses a Quicksort algorithm.

            Console.WriteLine(".Net Sort - Ticks/ Time in Milliseconds spent to create object and sort the array: " + watch.Elapsed.Ticks);
        }
Ejemplo n.º 8
0
        static void Main(string[] args)
        {
            RegisterDependencies();
            int[] unsortedNumbers;

            using (var scope = Container.BeginLifetimeScope())
            {
                _timerService = scope.Resolve <ITimerService>();

                unsortedNumbers = new int[] { 7, 2, 1, 6, 8, 5, 3, 4 };
                var heapSort = new HeapSort(_timerService);
                Sort(heapSort, unsortedNumbers);

                unsortedNumbers = new int[] { 7, 2, 1, 6, 8, 5, 3, 4 };
                var mergeSort = new MergeSort(_timerService);
                Sort(mergeSort, unsortedNumbers);

                unsortedNumbers = new int[] { 7, 2, 1, 6, 8, 5, 3, 4 };
                var quickSort = new QuickSort(_timerService);
                Sort(quickSort, unsortedNumbers);
            }
        }
Ejemplo n.º 9
0
        public static void Sort(uint[] UnsortedArray, int Length, SortNames AlgorithmName)
        {
            ISort Algorithm = null;

            switch (AlgorithmName)
            {
            case (SortNames.SelectionSort):
                Algorithm = new SelectionSort();
                break;

            case (SortNames.InsertionSort):
                Algorithm = new InsertionSort();
                break;

            case (SortNames.ShellSort1):
                Algorithm = new ShellSort1();
                break;

            case (SortNames.ShellSort2):
                Algorithm = new ShellSort2();
                break;

            case (SortNames.QuickSort):
                Algorithm = new QuickSort();
                break;

            case (SortNames.MergeSort):
                Algorithm = new MergeSort();
                break;

            case (SortNames.RadixSort):
                Algorithm = new RadixSort();
                break;
            }
            Algorithm.Sort(UnsortedArray, Length);
        }
Ejemplo n.º 10
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();
        }
Ejemplo n.º 11
0
        private void QuickSortButton_Click(object sender, EventArgs e)
        {
            var quick = new QuickSort <SortedItem>(items);

            Button_Click(quick);
        }
Ejemplo n.º 12
0
        static void Main(string[] args)
        {
            //Generate random 1D array

            int[] myArray     = ArrayGenerator();
            int[] myTempArray = new int[myArray.Length];
            Array.Copy(myArray, myTempArray, myArray.Length);

            //Print the unsorted list to the console so it can be checked

            Console.WriteLine("Unsorted list:");

            foreach (int element in myArray)
            {
                Console.WriteLine(element);
            }

            Console.WriteLine();

            //Calling different sorting algorithms on the list and putting the results into TXTs

            Console.WriteLine("Check on array that has duplicates:");
            Console.WriteLine();

            BubbleSort.BubbleSortAlgorithm(myArray);
            myTempArray.CopyTo(myArray, 0);

            SelectionSort.SelectionSortAlgorithm(myArray);
            myTempArray.CopyTo(myArray, 0);

            //MergeSort.MergeSortAlgorithm(myArray);
            myTempArray.CopyTo(myArray, 0);

            InsertionSort.InsertionSortAlgorithm(myArray);
            myTempArray.CopyTo(myArray, 0);

            BucketSort.BucketSortAlgorithm(myArray);
            myTempArray.CopyTo(myArray, 0);

            QuickSort.QuickSortAlgorithm(myArray);
            myTempArray.CopyTo(myArray, 0);

            GnomeSort.GnomeSortAlgorithm(myArray);
            myTempArray.CopyTo(myArray, 0);

            CombSort.CombSortAlgorithm(myArray);
            myTempArray.CopyTo(myArray, 0);

            CountingSort.CountingSortAlgorithm(myArray);
            myTempArray.CopyTo(myArray, 0);

            HeapSort.HeapSortAlgorithm(myArray);
            myTempArray.CopyTo(myArray, 0);

            //CycleSort.CycleSortAlgorithm(myArray);
            myTempArray.CopyTo(myArray, 0);

            //BogoSort.BogoSortAlgorithm(myArray);
            myTempArray.CopyTo(myArray, 0);

            CocktailSort.CocktailSortAlgorithm(myArray);
            myTempArray.CopyTo(myArray, 0);

            StoogeSort.StoogeSortAlgorithm(new int[] { 2, 4, 5, 3, 1 });
            myTempArray.CopyTo(myArray, 0);

            OddEvenSort.OddEvenSortAlgorithm(myArray);
            myTempArray.CopyTo(myArray, 0);

            BubbleSortRecursive.BubblesortRecursiveAlgorithm(myArray);
            myTempArray.CopyTo(myArray, 0);

            //InsertionSortRecursive.InsertionSortRecursiveAlgorithm(myArray);
            myTempArray.CopyTo(myArray, 0);

            //ShellSort.ShellSortAlgorithm(myArray);
            myTempArray.CopyTo(myArray, 0);

            StrandSort.StrandSortAlgorithm(new int[] { 3, 5, 5, 1, 5, 3, 10, 8, 9 });
            myTempArray.CopyTo(myArray, 0);

            //

            Console.ReadKey();
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Sort the specified array with the algorithm of index i.
        /// </summary>
        /// <returns>The sort.</returns>
        /// <param name="a">The array of integers.</param>
        /// <param name="i">The index of the algorithm.</param>
        static void sort(int[] a, int i)
        {
            switch (i)
            {
            // Bubble Sort.
            case 1:
                // Stores the memory allocated at the moment.
                var mem1 = GC.GetTotalMemory(false);

                // Starts a new stopwatch.
                var watch = System.Diagnostics.Stopwatch.StartNew();

                // Calls the Bubble Sort algorithm.
                BubbleSort.sort(a);

                // Stops the stopwatch.
                watch.Stop();

                // Gets the memory allocated at the moment and sumtracts the
                // older one to get the memory used in the intermediate process.
                var mem2 = GC.GetTotalMemory(false) - mem1;

                // Writes the memory usage in the index of its algorithm in the memory array.
                MainClass.mem[i] = mem2;

                // Gets the value of the stopwatch and stores itin the running time array.
                double time = watch.ElapsedTicks;
                MainClass.eff[i] = time;
                break;

            // Similarly the other cases.
            case 2:
                mem1  = GC.GetTotalMemory(false);
                watch = System.Diagnostics.Stopwatch.StartNew();
                InsertSort.sort(a);
                watch.Stop();
                mem2             = GC.GetTotalMemory(false) - mem1;
                MainClass.mem[i] = mem2;
                time             = watch.ElapsedTicks;
                MainClass.eff[i] = time;
                break;

            case 3:
                mem1  = GC.GetTotalMemory(false);
                watch = System.Diagnostics.Stopwatch.StartNew();
                QuickSort.sort(a);
                watch.Stop();
                mem2             = GC.GetTotalMemory(false) - mem1;
                MainClass.mem[i] = mem2;
                time             = watch.ElapsedTicks;
                MainClass.eff[i] = time;
                break;

            case 4:
                mem1  = GC.GetTotalMemory(false);
                watch = System.Diagnostics.Stopwatch.StartNew();
                HeapSort.sort(a);
                watch.Stop();
                mem2             = GC.GetTotalMemory(false) - mem1;
                MainClass.mem[i] = mem2;
                time             = watch.ElapsedTicks;
                MainClass.eff[i] = time;
                break;

            case 5:
                mem1  = GC.GetTotalMemory(false);
                watch = System.Diagnostics.Stopwatch.StartNew();
                MergeSort.sort(a);
                watch.Stop();
                mem2             = GC.GetTotalMemory(false) - mem1;
                MainClass.mem[i] = mem2;
                time             = watch.ElapsedTicks;
                MainClass.eff[i] = time;
                break;
            }
        }
Ejemplo n.º 14
0
        static void Main(string[] args)
        {
            Random random = new Random();

            Console.WriteLine("Enter the Number");

            int c = Console.Read();

            int[] arr = new int[c];

            for (int i = 0; i < c; i++)
            {
                arr[i] = random.Next();
            }


            Console.WriteLine("Choose The Algorhitm ");

            Console.WriteLine("1: InsertSort");

            Console.WriteLine("2: BubbleSort");

            Console.WriteLine("3: Quick Sort");

            Console.WriteLine("4: Heap Sort");

            Console.WriteLine("5: Merge Sort");

            Console.WriteLine("6: All");



            string s = Console.ReadLine();

            if (s.Length > 1)
            {
                for (int i = Convert.ToInt32(s.Substring(0, 1)); i <= Convert.ToInt32(s.Substring(2, 3)); i++)
                {
                    switch (Convert.ToString(i))
                    {
                    case "1":
                        InsertSort.Sort(arr);
                        break;

                    case "2":
                        BubbleSort.Sort(arr);
                        break;

                    case "3":
                        QuickSort.Sort(arr, 0, arr.Length - 1);
                        break;

                    case "4":
                        HeapSort.Sort(ref arr);
                        break;

                    case "5":
                        MergeSort.Sort(ref arr, 0, arr.Length - 1);
                        break;
                    }
                }
            }
            else
            {
                switch (s)
                {
                case "1":
                    InsertSort.Sort(arr);
                    break;

                case "2":
                    BubbleSort.Sort(arr);
                    break;

                case "3":
                    QuickSort.Sort(arr, 0, arr.Length - 1);
                    break;

                case "4":
                    HeapSort.Sort(ref arr);
                    break;

                case "5":
                    MergeSort.Sort(ref arr, 0, arr.Length - 1);
                    break;

                case "6":
                    InsertSort.Sort(arr);
                    BubbleSort.Sort(arr);
                    QuickSort.Sort(arr, 0, arr.Length - 1);
                    HeapSort.Sort(ref arr);
                    MergeSort.Sort(ref arr, 0, arr.Length - 1);
                    break;
                }
            }

            Console.ReadLine();
        }
Ejemplo n.º 15
0
        static void Main(string[] args)
        {
            int size  = 20000;
            int range = 10000;

            BubbleSort bubbleSort = new BubbleSort();

            int[] unsortedArray1 = Utils.generateArray(size, range);

            var watch = System.Diagnostics.Stopwatch.StartNew();

            bubbleSort.bubbleSort(unsortedArray1);
            watch.Stop();
            var elapsedMs = watch.Elapsed.TotalMilliseconds;

            Console.WriteLine("bubble took time: " + elapsedMs);

            CountSort countSort = new CountSort();

            int[] unsortedArray2 = Utils.generateArray(size, range);

            watch = System.Diagnostics.Stopwatch.StartNew();
            countSort.countSort(unsortedArray2);
            watch.Stop();
            elapsedMs = watch.Elapsed.TotalMilliseconds;

            Console.WriteLine("count took time: " + elapsedMs);

            MergeSort mergeSort = new MergeSort();

            int[] unsortedArray3 = Utils.generateArray(size, range);

            watch = System.Diagnostics.Stopwatch.StartNew();
            MergeSort.sort(unsortedArray3, 0, unsortedArray3.Length - 1);
            watch.Stop();
            elapsedMs = watch.Elapsed.TotalMilliseconds;

            Console.WriteLine("merge took time: " + elapsedMs);

            InsertionSort insertionSort = new InsertionSort();

            int[] unsortedArray4 = Utils.generateArray(size, range);

            watch = System.Diagnostics.Stopwatch.StartNew();
            insertionSort.insertionSort(unsortedArray4);
            watch.Stop();
            elapsedMs = watch.Elapsed.TotalMilliseconds;

            Console.WriteLine("insertion took time: " + elapsedMs);

            QuickSort quickSort = new QuickSort();

            int[] unsortedArray5 = Utils.generateArray(size, range);

            watch = System.Diagnostics.Stopwatch.StartNew();
            quickSort.quickSort(unsortedArray5);
            watch.Stop();
            elapsedMs = watch.Elapsed.TotalMilliseconds;

            Console.WriteLine("quick took time: " + elapsedMs);


            HeapSort heapSort = new HeapSort();

            int[] unsortedArray6 = Utils.generateArray(size, range);

            watch = System.Diagnostics.Stopwatch.StartNew();
            heapSort.heapSort(unsortedArray6);
            watch.Stop();
            elapsedMs = watch.Elapsed.TotalMilliseconds;

            Console.WriteLine("heap took time: " + elapsedMs);

            SelectionSort selectionSort = new SelectionSort();

            int[] unsortedArray7 = Utils.generateArray(size, range);

            watch = System.Diagnostics.Stopwatch.StartNew();
            selectionSort.selectionSort(unsortedArray7);
            watch.Stop();
            elapsedMs = watch.Elapsed.TotalMilliseconds;

            Console.WriteLine("selection took time: " + elapsedMs);

            Console.ReadKey();
        }
Ejemplo n.º 16
0
        private static void SortWithEveryMethod(List <int> data, System.Diagnostics.Stopwatch watch)
        {
            TimeSpan      ts;
            List <double> time = new List <double>();

            Console.WriteLine("Insertion Sorting:");
            Console.Write("Ascending: ");
            watch.Start();
            List <int> insSrtAsc = InsertionSorting.SortAscending(data);

            watch.Stop();
            ts = watch.Elapsed;
            time.Add(ts.TotalMilliseconds);
            watch.Reset();
            foreach (int i in insSrtAsc)
            {
                Console.Write(i + " ");
            }
            Console.Write("\nDescending: ");
            watch.Start();
            List <int> insSrtDesc = InsertionSorting.SortDescending(data);

            watch.Stop();
            ts = watch.Elapsed;
            time.Add(ts.TotalMilliseconds);
            watch.Reset();
            foreach (int i in insSrtDesc)
            {
                Console.Write(i + " ");
            }

            Console.WriteLine("\nBubble Sorting:");
            Console.Write("Ascending: ");
            watch.Start();
            List <int> bubSrtAsc = BubbleSorting.SortAscending(data);

            watch.Stop();
            ts = watch.Elapsed;
            time.Add(ts.TotalMilliseconds);
            watch.Reset();
            foreach (int i in bubSrtAsc)
            {
                Console.Write(i + " ");
            }
            Console.Write("\nDescending: ");
            watch.Start();
            List <int> bubSrtDesc = BubbleSorting.SortDescending(data);

            watch.Stop();
            ts = watch.Elapsed;
            time.Add(ts.TotalMilliseconds);
            watch.Reset();
            foreach (int i in bubSrtDesc)
            {
                Console.Write(i + " ");
            }

            Console.WriteLine("\nSelection Sorting:");
            Console.Write("Ascending: ");
            watch.Start();
            List <int> selSrtAsc = SelectionSorting.SortAscending(data);

            watch.Stop();
            ts = watch.Elapsed;
            time.Add(ts.TotalMilliseconds);
            watch.Reset();
            foreach (int i in selSrtAsc)
            {
                Console.Write(i + " ");
            }
            Console.Write("\nDescending: ");
            watch.Start();
            List <int> selSrtDesc = SelectionSorting.SortDescending(data);

            watch.Stop();
            ts = watch.Elapsed;
            time.Add(ts.TotalMilliseconds);
            watch.Reset();
            foreach (int i in selSrtDesc)
            {
                Console.Write(i + " ");
            }

            Console.WriteLine("\nMerge sorting:");
            Console.Write("Ascending: ");
            watch.Start();
            List <int> merSrtAsc = MergeSorting.SortAscending(data);

            watch.Stop();
            ts = watch.Elapsed;
            time.Add(ts.TotalMilliseconds);
            watch.Reset();
            foreach (int i in merSrtAsc)
            {
                Console.Write(i + " ");
            }
            Console.Write("\nDescending: ");
            watch.Start();
            List <int> merSrtDesc = MergeSorting.SortDescending(data);

            watch.Stop();
            ts = watch.Elapsed;
            time.Add(ts.TotalMilliseconds);
            watch.Reset();
            foreach (int i in merSrtDesc)
            {
                Console.Write(i + " ");
            }

            Console.WriteLine("\nCounting sorting:");
            Console.Write("Ascending: ");
            watch.Start();
            List <int> couSrtAsc = CountingSorting.SortAscending(data);

            watch.Stop();
            ts = watch.Elapsed;
            time.Add(ts.TotalMilliseconds);
            watch.Reset();
            foreach (int i in couSrtAsc)
            {
                Console.Write(i + " ");
            }
            Console.Write("\nDescending: ");
            watch.Start();
            List <int> couSrtDesc = CountingSorting.SortDescending(data);

            watch.Stop();
            ts = watch.Elapsed;
            time.Add(ts.TotalMilliseconds);
            watch.Reset();
            foreach (int i in couSrtDesc)
            {
                Console.Write(i + " ");
            }

            Console.WriteLine("\nBucket sorting:");
            Console.Write("Ascending: ");
            watch.Start();
            List <int> bucSrtAsc = BucketSorting.SortAscending(data);

            watch.Stop();
            ts = watch.Elapsed;
            time.Add(ts.TotalMilliseconds);
            watch.Reset();
            foreach (int i in bucSrtAsc)
            {
                Console.Write(i + " ");
            }
            Console.Write("\nDescending: ");
            watch.Start();
            List <int> bucSrtDesc = BucketSorting.SortDescending(data);

            watch.Stop();
            ts = watch.Elapsed;
            time.Add(ts.TotalMilliseconds);
            watch.Reset();
            foreach (int i in bucSrtDesc)
            {
                Console.Write(i + " ");
            }

            Console.WriteLine("\nQuickSort:");
            Console.Write("Ascending: ");
            watch.Start();
            List <int> quiSrtAsc = QuickSort.SortAscending(data);

            watch.Stop();
            ts = watch.Elapsed;
            time.Add(ts.TotalMilliseconds);
            watch.Reset();
            foreach (int i in quiSrtAsc)
            {
                Console.Write(i + " ");
            }
            Console.Write("\nDescending: ");
            watch.Start();
            List <int> quiSrtDesc = QuickSort.SortDescending(data);

            watch.Stop();
            ts = watch.Elapsed;
            time.Add(ts.TotalMilliseconds);
            watch.Reset();
            foreach (int i in quiSrtDesc)
            {
                Console.Write(i + " ");
            }

            Console.WriteLine("\nHeapSort:");
            Console.Write("Ascending: ");
            watch.Start();
            List <int> heaSrtAsc = HeapSorting.SortAscending(data);

            watch.Stop();
            ts = watch.Elapsed;
            time.Add(ts.TotalMilliseconds);
            watch.Reset();
            foreach (int i in heaSrtAsc)
            {
                Console.Write(i + " ");
            }
            Console.Write("\nDescending: ");
            watch.Start();
            List <int> heaSrtDesc = HeapSorting.SortDescending(data);

            watch.Stop();
            ts = watch.Elapsed;
            time.Add(ts.TotalMilliseconds);
            watch.Reset();
            foreach (int i in heaSrtDesc)
            {
                Console.Write(i + " ");
            }
            Console.WriteLine();
            Console.WriteLine("time:");
            for (int i = 0; i < time.Count; i++)
            {
                switch (i)
                {
                case 0:
                    Console.Write("Insertion: ");
                    break;

                case 2:
                    Console.Write("\nBubble: ");
                    break;

                case 4:
                    Console.Write("\nSelection: ");
                    break;

                case 6:
                    Console.Write("\nMerge: ");
                    break;

                case 8:
                    Console.Write("\nCounting: ");
                    break;

                case 10:
                    Console.Write("\nBucket: ");
                    break;

                case 12:
                    Console.Write("\nQuickSort: ");
                    break;

                case 14:
                    Console.Write("\nHeap: ");
                    break;

                default:
                    Console.Write(" ");
                    break;
                }
                Console.Write(time[i]);
            }
        }
Ejemplo n.º 17
0
 /// <summary>
 /// Sort the specified array.
 /// </summary>
 /// <returns>The sort.</returns>
 /// <param name="a">The array.</param>
 public static void sort(int[] a)
 {
     QuickSort.sortik(a, 0, a.Length - 1);
 }