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)); }
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(); }
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(); }