public void Sort(int[] target) { if (target is null) { return; } var swapped = true; var gap = target.Length; while (gap > 1 || swapped) { if (gap > 1) { // 1.3 is kinda recommended value for the gap reduction. gap = (int)(gap / 1.3); } var j = 0; swapped = false; while (j + gap < target.Length) { if (target[j] > target[j + gap]) { SortUtilities.Swap(target, j, j + gap); swapped = true; } j++; } } }
private static int Partition(int[] input, int start, int end) { var mid = start + ((end - start) / 2); var leftPointer = start - 1; var rightPointer = end + 1; while (true) { do { leftPointer++; } while (input[leftPointer] < input[mid]); do { rightPointer--; } while (input[rightPointer] > input[mid]); if (rightPointer <= leftPointer) { return(rightPointer); } SortUtilities.Swap(input, leftPointer, rightPointer); } }
private static int Partition(int[] input, int start, int end) { var pivot = input[start + ((end - start) / 2)]; var leftPointer = start - 1; var rightPointer = end + 1; while (true) { do { leftPointer++; } while (input[leftPointer] < pivot); do { rightPointer--; } while (input[rightPointer] > pivot); if (leftPointer >= rightPointer) { return(rightPointer); } SortUtilities.Swap(input, leftPointer, rightPointer); } }
private static int Partition(int[] input, int start, int end) { var pivot = input[end]; var seen = start - 1; for (var frontier = start; frontier < end + 1; frontier++) { if (input[frontier] <= pivot) { seen += 1; SortUtilities.Swap(input, seen, frontier); } } return(seen); }
public void Sort(int[] target) { if (target is null) { return; } // (target.Length / 2) - 1 - this will remove all leaves from the build, they are trivial and has no children // to they are already heapified for (var i = (target.Length / 2) - 1; i >= 0; i--) { BuildMaxHeapInPlace(target, target.Length, i); } for (var i = target.Length - 1; i >= 0; i--) { // Move current root to the end SortUtilities.Swap(target, 0, i); BuildMaxHeapInPlace(target, i, 0); } }
/* Characteristics: * Complexity: o(n^2) * Stable: YES * In-place: YES * Online: NO * Advantages: * 1. Simple implementation * 2. Makes the minimum possible number of swaps, n − 1 in the worst case. */ public void Sort(int[] target) { if (target is null) { return; } for (var i = 0; i < target.Length; i++) { var smallestIndex = i; for (var j = i; j < target.Length; j++) { if (target[smallestIndex] > target[j]) { smallestIndex = j; } } SortUtilities.Swap(target, smallestIndex, i); } }
private static void BuildMaxHeapInPlace(int[] target, int heapSize, int root) { var largestIndexInSubtree = root; var leftChildIndex = (root * 2) + 1; var rightChildIndex = (root * 2) + 2; if (heapSize > leftChildIndex && target[largestIndexInSubtree] < target[leftChildIndex]) { largestIndexInSubtree = leftChildIndex; } if (heapSize > rightChildIndex && target[largestIndexInSubtree] < target[rightChildIndex]) { largestIndexInSubtree = rightChildIndex; } if (largestIndexInSubtree != root) { SortUtilities.Swap(target, root, largestIndexInSubtree); BuildMaxHeapInPlace(target, heapSize, largestIndexInSubtree); } }
/* Characteristics: * Complexity: o(n^2) * Stable: YES * In-place: YES * Online: YES * Advantages: * 1. Simple implementation */ public void Sort(int[] target) { if (target is null) { return; } bool swapped; var j = 0; do { swapped = false; for (var i = 1; i < target.Length - j; i++) { if (target[i - 1] > target[i]) { SortUtilities.Swap(target, i, i - 1); swapped = true; } } j++; } while (swapped); }
public static void SortIntrospective <T, TComparer, TValue>(T[] keys, TComparer comparer, IntT start, IntT count, TValue[] values) where TComparer : IComparer <T> { Contract.Requires(null != keys); Contract.Requires(0 <= start); Contract.Requires(0 <= count); Contract.Requires(count <= keys.Length); Contract.Requires(count + start <= keys.Length); Contract.Requires(null == values || count + start <= values.Length); if (count < 2) { return; } SortPartIntrospectiveRecursionLimited(keys, comparer, start, unchecked (count + start - 1), unchecked (2 * SortUtilities.FloorLog2(keys.Length)), values); }