private static void BubbleSortTest() { Console.WriteLine("Bubble Sort Test:"); int[] arr = new int[_arr.Length]; Array.Copy(_arr, arr, _arr.Length); ISortAble bubbleSort = new BubbleSort(); bubbleSort.StartTimer(); int[] sorted = bubbleSort.Sort(arr); bubbleSort.StopTimer(); if (_printArray) { bubbleSort.PrintArray(sorted); } bubbleSort.PrintElapsedTime(); bubbleSort.ResetTimer(); }
static int Main(string[] args) { if (args.Length == 0 || !algorithms.Contains(args[0])) { Console.WriteLine("Please select a sorting algorithm."); Console.WriteLine("Usage: <quicksort|mergesort|timsort|heapsort|bubble-sort|insertion-sort|selection-sort|tree-sort|shell-sort|bucket-sort>"); return(1); } Console.WriteLine("Please enter a list of numbers:"); var chars = Console.ReadLine().Split(' '); int[] nums = new int[chars.Length]; for (int i = 0; i < chars.Length; i++) { int num; bool success = Int32.TryParse(chars[i], out num); if (!success) { Console.WriteLine("List must only contain numbers."); return(1); } else { nums[i] = num; } } int swaps = 0; string algorithm = args[0]; switch (algorithm) { case "bubble-sort": swaps = BubbleSort.Sort(nums); break; case "mergesort": MergeSort.Sort(nums, 0, nums.Length - 1); break; default: Console.WriteLine("Algorithm not implemented yet."); return(1); } Console.Write("Sorted list: "); for (int i = 0; i < nums.Length; i++) { Console.Write($"{nums[i]} "); } if (algorithm == "bubble-sort") { Console.WriteLine(); Console.WriteLine($"List is sorted in {swaps} swaps."); } return(0); }
public void BubbleSortSortsArray() { var testCollection = TestUtils.GetUnsortedCharArray(); var algorithm = new BubbleSort(); algorithm.Sort(testCollection); Assert.True(TestUtils.IsSorted(testCollection)); }
static void Main(string[] args) { var sorter = new BubbleSort(); var result = BubbleSort.Sort(new int[] { 29, 27, 3, 11, 18, 28, 13 }); var output = string.Join(",", result.Select(p => p.ToString()).ToArray()); Console.WriteLine(output); }
private static void ExecuteBubbleSort() { var array = new int[] { 7, 2, 5, 9, 1, 13, 3 }; Console.WriteLine($"Bubble sort - {string.Join(",", array)}"); var sorter = new BubbleSort(); sorter.Sort(array); Console.WriteLine($"Sorted array - {string.Join(",", array)}"); }
static void Main(string[] args) { int size = 20; CustomArray arrOfNums = new CustomArray(size); CustomArray sortedArray = new CustomArray(size); Random rnd = new Random(100); for (int i = 0; i < size; i++) { arrOfNums.Insert((int)(rnd.NextDouble() * 100)); } BubbleSort bs = new BubbleSort(arrOfNums); bs.Sort(); Console.ReadLine(); }
/// <summary> /// Array sorting using comprer and indexer. /// </summary> /// <param name="array">The array of int.</param> /// <param name="comparer">The comparison principle.</param> /// <param name="indexer">Indexer.</param> /// <exception cref="ArgumentNullException">Throw when array, comparer or indexer is null.</exception> public static void SortTheArray(int[] array, IComparer comparer, IIndexer indexer) { if (array == null) { throw new ArgumentNullException($"{nameof(array)} can't be null."); } if (comparer == null) { throw new ArgumentNullException($"{nameof(comparer)} can't be null."); } if (indexer == null) { throw new ArgumentNullException($"{nameof(indexer)} can't be null."); } BubbleSort.Sort(array, comparer, indexer); }
static void Main(string[] args) { int[] array = { 5, 12, 1, 88, 21, 42, 2 }; //SelectionSort sort = new SelectionSort(array); BubbleSort sort = new BubbleSort(array); //print out array before sorting sort.PrintArray(array); sort.Sort(); sort.PrintArray(array); // Keep the console window open in debug mode. Console.WriteLine("\nPress any key to exit."); Console.ReadKey(); }
public void BubbleSortTest() { // Arrange var arrayCount = 0; foreach (var sample in GenerateSampleArrays()) { // Act BubbleSort.Sort(sample); // Assert for (int i = 0; i < sample.Length - 1; i++) { Assert.True(sample[i] <= sample[i + 1], $"Array #{arrayCount} was not sorted."); arrayCount++; } } }
static void Main(string[] args) { // bubble sort var array = new int[] { 1, 4, 3, 7, 44, 34, 6, 11, 36, 43 }; var sorted = BubbleSort.Sort(array); foreach (int item in sorted) { Console.Write(item.ToString()); } // binary search var myArray = new int[] { 1, 2, 4, 6, 10, 14, 15, 19, 20, 34, 36, 38, 40, 42 }; var value = BinarySearch.BSearch(10, myArray); // merge sort int[] mergeSorted = MergeSort.Sort(array); Console.ReadKey(); }
static void Main(string[] args) { Console.WriteLine("Warmup"); var stopwatch = Stopwatch.StartNew(); long seed = Environment.TickCount; // Prevents the JIT Compiler // from optimizing Fkt calls away long result = 0; int count = 100000000; while (stopwatch.ElapsedMilliseconds < 1200) // A Warmup of 1000-1500 mS // stabilizes the CPU cache and pipeline. { result = TestFunction(seed, count); // Warmup } stopwatch.Stop(); int arraySize = 1000000; var runCount = 2; stopwatch.Reset(); stopwatch.Start(); var stringArray = GetStringArray(arraySize); stopwatch.Stop(); Console.WriteLine($"Initializing a {arraySize} elements string array: {stopwatch.ElapsedMilliseconds} ms"); stopwatch.Reset(); stopwatch.Start(); var intArray = GetIntArray(arraySize); stopwatch.Stop(); Console.WriteLine($"Initializing a {arraySize} elements Integer array: {stopwatch.ElapsedMilliseconds} ms"); stopwatch.Reset(); stopwatch.Start(); var stringList = GetStringArrayList(arraySize); stopwatch.Stop(); Console.WriteLine($"Initializing a {arraySize} elements string List: {stopwatch.ElapsedMilliseconds} ms"); var currentCount = 0; Console.WriteLine("-------------------------------------------------------------"); Console.WriteLine("-------------- Integer Bubble Sort ---------------------"); Console.WriteLine("-------------------------------------------------------------"); while (currentCount < runCount) { stopwatch.Reset(); stopwatch.Start(); BubbleSort.Sort(intArray = GetIntArray(arraySize)); stopwatch.Stop(); Console.WriteLine($"Bubble Sorting an Integer array of {arraySize} elements {stopwatch.ElapsedMilliseconds} ms"); //PrintTop10(intArray); currentCount++; } currentCount = 0; Console.WriteLine("-------------------------------------------------------------"); Console.WriteLine("------------ Integer Bubble Sort (Parallel) ----------------"); Console.WriteLine("-------------------------------------------------------------"); while (currentCount < runCount) { stopwatch.Reset(); stopwatch.Start(); BubbleSort.sortParallel(GetIntArray(arraySize)); stopwatch.Stop(); Console.WriteLine($"Bubble Sorting an Integer array of {arraySize} elements {stopwatch.ElapsedMilliseconds} ms"); //PrintTop10(intArray); currentCount++; } currentCount = 0; Console.WriteLine("-------------------------------------------------------------"); Console.WriteLine("-------------- Integer Merge Sort ---------------------"); Console.WriteLine("-------------------------------------------------------------"); while (currentCount < runCount) { stopwatch.Reset(); stopwatch.Start(); MergeSort.Sort(GetIntArray(arraySize)); stopwatch.Stop(); Console.WriteLine($"Merge Sorting an Integer array of {arraySize} elements {stopwatch.ElapsedMilliseconds} ms"); //PrintTop10(intArray); currentCount++; } //currentCount = 0; //Console.WriteLine("-------------------------------------------------------------"); //Console.WriteLine("-------------- String Bubble Sort ---------------------"); //Console.WriteLine("-------------------------------------------------------------"); //while (currentCount < runCount) //{ // stopwatch.Reset(); // stopwatch.Start(); // BubbleSort.Sort(GetStringArray(arraySize)); // stopwatch.Stop(); // Console.WriteLine($"Bubble Sorting a string array of {arraySize} elements {stopwatch.ElapsedMilliseconds} ms"); // //PrintTop10(stringArray); // currentCount++; //} currentCount = 0; Console.WriteLine("-------------------------------------------------------------"); Console.WriteLine("-------------- String Merge Sort ---------------------"); Console.WriteLine("-------------------------------------------------------------"); while (currentCount < runCount) { stopwatch.Reset(); stopwatch.Start(); MergeSort.Sort(GetStringArray(arraySize)); stopwatch.Stop(); Console.WriteLine($"Bubble Sorting a string array of {arraySize} elements {stopwatch.ElapsedMilliseconds} ms"); //PrintTop10(stringArray); currentCount++; } currentCount = 0; Console.WriteLine("-------------------------------------------------------------"); Console.WriteLine("-------------- String List Merge Sort ---------------------"); Console.WriteLine("-------------------------------------------------------------"); while (currentCount < runCount) { stopwatch.Reset(); stopwatch.Start(); MergeSort.Sort(GetStringArrayList(arraySize)); stopwatch.Stop(); Console.WriteLine($"Bubble Sorting a string array of {arraySize} elements {stopwatch.ElapsedMilliseconds} ms"); //PrintTop10(stringArray); currentCount++; } Console.Read(); }