public void BaseTest() { ArrayPrinter arrayPrinter = new ArrayPrinter(); this.arrayGenerator.RandomGenerator(this.Length); this.testArray = this.arrayGenerator.output; arrayPrinter.Print(this.testArray); this.mergeSort.Sort(this.testArray); arrayPrinter.Print(this.testArray); for (int i = 0; i < this.Length - 1; i++) { Assert.LessOrEqual(this.testArray[i], this.testArray[i + 1]); } }
public static int[] SortedSquares(int[] A) { var output = new int[A.Length]; //Approach 1: Square each element in the array and then sort the squares. //Approach 2: //if array contains negative numbers, take their modulus and sort it again //Approach 3: 2 pointer approach // Example -4 -1 0 2 3 int left = 0; int right = A.Length - 1; int resultArrayPointer = output.Length - 1; for (resultArrayPointer = A.Length - 1; resultArrayPointer > 0; resultArrayPointer--) { if (Math.Abs(A[left]) > Math.Abs(A[right])) { output[resultArrayPointer] = A[left] * A[left]; left++; } else { output[resultArrayPointer] = A[right] * A[right]; right--; } } ArrayPrinter.Print(output); return(output); }
private static void ThreeWayOrdering(int[] input) { int low = 10; int mid = 30; int high = 31; int i = 0, j = 0; int k = input.Length - 1; while (j <= k) { if (input[j] < mid) { //swap var temp = input[i]; input[i] = input[j]; input[j] = temp; i++; j++; } else if (input[j] > mid) { //swap var temp = input[j]; input[j] = input[k]; input[k] = temp; k = k - 1; } else { j++; } } ArrayPrinter.Print(input); }