private static void SortingAlgorithms() { var inputArr = new int[5] { 2, 3, 1, 5, 6 }; #region OrderBy Range Problem using MergeSort Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); SortingProblems sortingProblems = new SortingProblems(); Console.WriteLine("Given: {0}", inputArr.ToString()); int i = 1, j = 3; sortingProblems.SortSelectedRanges <int[]>(inputArr, i, j); Console.WriteLine("Sorted: {0} i:{1}, j:{2}", inputArr.ToString(), i, j); stopwatch.Stop(); Console.WriteLine("RunTime " + stopwatch.ElapsedMilliseconds); #endregion #region Merge sorting MergeSorting ms = new MergeSorting(); Console.WriteLine("Input Arrays:"); inputArr.All(a => { Console.Write("{0} ", a); return(true); }); Console.WriteLine(); Console.WriteLine("Sorted Arrays:"); foreach (var item in ms.Run <int[]>(inputArr)) { Console.Write("{0} ", item); } #endregion }
public void PassingSortingByRange() { #region prepare random data int n = 1000; Random r = new Random(); var inputArr = new int[n]; const int min = -10; const int max = 10000; for (int z = 0; z < n; z++) { inputArr[z] = r.Next(min, max); } int i = r.Next(10, 400), j = r.Next(400, 800); #endregion Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); SortingProblems sortingProblems = new SortingProblems(); Debug.WriteLine("Given: " + String.Join(",", inputArr)); var result = sortingProblems.SortSelectedRanges <int[]>(inputArr, i, j); Debug.WriteLine("Sorted: {0} i:{1}, j:{2}", String.Join(",", result), i, j); stopwatch.Stop(); Assert.IsTrue(inputArr.Length == result.Length); Debug.WriteLine("RunTime " + stopwatch.ElapsedMilliseconds); }
static void Main(string[] args) { Console.WriteLine("Hello World!"); //int[] nums = new int[] { 3, 2, 2, 3 }; //nums = Array.FindAll(nums, i => i != 3); int[] arr = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; Console.WriteLine("The index of 7 using sequential search: " + SearchUtil.sequential_search(arr, arr.Length, 7)); Console.WriteLine("The index of 7 using binary search: " + SearchUtil.binary_search(arr, arr.Length, 7)); int[] array = new int[] { 9, 1, 8, 2, 7, 3, 6, 4, 5 }; BubbleSort.sort2(array); Console.Write("Bubble sort : "); for (int i = 0; i < array.Length; i++) { Console.Write(array[i] + " "); } int[] array2 = new int[] { 9, 1, 8, 2, 7, 3, 6, 4, 5 }; Console.WriteLine(); SelectionSort.sort(array2); Console.Write("Insertion sort : "); for (int i = 0; i < array2.Length; i++) { Console.Write(array2[i] + " "); } int[] array1 = new int[] { 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1 }; SortingProblems.PartitionBI(array1, 0, 1); Console.WriteLine(); Console.Write("Partitioning : "); for (int i = 0; i < array1.Length; i++) { Console.Write(array1[i] + " "); } int[] arr2 = new int[] { 0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1 }; SortingProblems.PartitionTRI(arr2, 0, 2); Console.WriteLine(); Console.Write("PartitioningTRI : "); for (int i = 0; i < arr2.Length; i++) { Console.Write(arr2[i] + " "); } array = new int[] { 9, 1, 8, 2, 7, 3, 6, 4, 5 }; SortingProblems.AbsBubbleSort(array, 5); printArray(array); arr = new int[] { 2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8 }; arr2 = new int[] { 2, 1, 8, 3 }; int[] result = SortingProblems.SortByArrayOrder(arr, arr2); Console.Write("SortByArrayOrder : "); printArray(result); }