/// <summary> /// Rearranges the array in ascending order, using the natural order.</summary> /// <param name="a">the array to be sorted</param> /// public static void Sort(IComparable[] a) { IComparable[] aux = new IComparable[a.Length]; a.CopyTo(aux, 0); Sort(aux, a, 0, a.Length - 1); Debug.Assert(OrderHelper.IsSorted(a)); }
/// <summary> /// Rearranges the array in ascending order, using the natural order.</summary> /// <param name="a">the array to be sorted</param> /// public static void Sort(IComparable[] a) { int N = a.Length; // put smallest element in position to serve as sentinel int exchanges = 0; for (int i = N - 1; i > 0; i--) { if (OrderHelper.Less(a[i], a[i - 1])) { OrderHelper.Exch(a, i, i - 1); exchanges++; } } if (exchanges == 0) return; // insertion sort with half-exchanges for (int i = 2; i < N; i++) { IComparable v = a[i]; int j = i; while (OrderHelper.Less(v, a[j - 1])) { a[j] = a[j - 1]; j--; } a[j] = v; } Debug.Assert(OrderHelper.IsSorted(a)); }
private static void Merge(IComparable[] src, IComparable[] dst, int lo, int mid, int hi) { // precondition: src[lo .. mid] and src[mid+1 .. hi] are sorted subarrays Debug.Assert(OrderHelper.IsSorted(src, lo, mid)); Debug.Assert(OrderHelper.IsSorted(src, mid + 1, hi)); int i = lo, j = mid + 1; for (int k = lo; k <= hi; k++) { if (i > mid) { dst[k] = src[j++]; } else if (j > hi) { dst[k] = src[i++]; } else if (OrderHelper.Less(src[j], src[i])) { dst[k] = src[j++]; // to ensure stability } else { dst[k] = src[i++]; } } // postcondition: dst[lo .. hi] is sorted subarray Debug.Assert(OrderHelper.IsSorted(dst, lo, hi)); }
/******************************************************************* * Version that takes Comparator as argument. *******************************************************************/ /// <summary> /// Rearranges the array in ascending order, using the provided order.</summary> /// <param name="a">the array to be sorted</param> /// <param name="comparator">the user comparator</param> /// public static void Sort <T>(T[] a, Comparer <T> comparator) { T[] aux = new T[a.Length]; a.CopyTo(aux, 0); Sort(aux, a, 0, a.Length - 1, comparator); Debug.Assert(OrderHelper.IsSorted(a, comparator)); }
/// <summary> /// Rearranges the array in ascending order, using the natural order.</summary> /// <param name="a">a the array to be sorted</param> /// public static void Sort(IComparable[] a) { int N = a.Length; // 3x+1 increment sequence: 1, 4, 13, 40, 121, 364, 1093, ... int h = 1; while (h < N / 3) { h = 3 * h + 1; } while (h >= 1) { // h-sort the array for (int i = h; i < N; i++) { for (int j = i; j >= h && OrderHelper.Less(a[j], a[j - h]); j -= h) { OrderHelper.Exch(a, j, j - h); } } Debug.Assert(IsHsorted(a, h)); h /= 3; } Debug.Assert(OrderHelper.IsSorted(a)); }
// quicksort the subarray a[lo .. hi] using 3-way partitioning private static void Sort(IComparable[] a, int lo, int hi) { if (hi <= lo) { return; } int lt = lo, gt = hi; IComparable v = a[lo]; int i = lo; while (i <= gt) { int cmp = a[i].CompareTo(v); if (cmp < 0) { OrderHelper.Exch(a, lt++, i++); } else if (cmp > 0) { OrderHelper.Exch(a, i, gt--); } else { i++; } } // a[lo..lt-1] < v = a[lt..gt] < a[gt+1..hi]. Sort(a, lo, lt - 1); Sort(a, gt + 1, hi); Debug.Assert(OrderHelper.IsSorted(a, lo, hi)); }
/// <summary>Rearranges the array in ascending order, using the natural order.</summary> /// <param name="a">the array to be sorted</param> /// public static void Sort(IComparable[] a) { int N = a.Length; for (int i = 1; i < N; i++) { // binary search to determine index j at which to insert a[i] IComparable v = a[i]; int lo = 0, hi = i; while (lo < hi) { int mid = lo + (hi - lo) / 2; if (OrderHelper.Less(v, a[mid])) { hi = mid; } else { lo = mid + 1; } } // insetion sort with "half exchanges" // (insert a[i] at index j and shift a[j], ..., a[i-1] to right) for (int j = i; j > lo; --j) { a[j] = a[j - 1]; } a[lo] = v; } Debug.Assert(OrderHelper.IsSorted(a)); }
/// <summary> /// Rearranges the subarray a[lo..hi] in ascending order, using the natural order.</summary> /// <param name="a">a the array to be sorted</param> /// <param name="lo">lo left endpoint</param> /// <param name="hi">hi right endpoint</param> /// public static void Sort(IComparable[] a, int lo, int hi) { for (int i = lo; i <= hi; i++) { for (int j = i; j > lo && OrderHelper.Less(a[j], a[j - 1]); j--) { OrderHelper.Exch(a, j, j - 1); } } Debug.Assert(OrderHelper.IsSorted(a, lo, hi)); }
/// <summary> /// Rearranges the subarray a[lo..hi] in ascending order, using a generic comparator.</summary> /// <param name="a">a the array</param> /// <param name="lo">lo left endpoint</param> /// <param name="hi">hi right endpoint</param> /// <param name="comparator">comparator the comparator specifying the order</param> /// public static void Sort <T>(T[] a, int lo, int hi, Comparer <T> comparator) { for (int i = lo; i <= hi; i++) { for (int j = i; j > lo && OrderHelper.Less(a[j], a[j - 1], comparator); j--) { OrderHelper.Exch <T>(a, j, j - 1); } } Debug.Assert(OrderHelper.IsSorted(a, lo, hi, comparator)); }
// quicksort the subarray from a[lo] to a[hi] private static void sort(IComparable[] a, int lo, int hi) { if (hi <= lo) { return; } int j = partition(a, lo, hi); sort(a, lo, j - 1); sort(a, j + 1, hi); Debug.Assert(OrderHelper.IsSorted(a, lo, hi)); }
/// <summary> /// Rearranges the array in ascending order, using a comparator.</summary> /// <param name="a">a the array</param> /// <param name="comparator">comparator the comparator specifying the order</param> /// public static void Sort(object[] a, System.Collections.Comparer comparator) { int N = a.Length; for (int i = 0; i < N; i++) { for (int j = i; j > 0 && OrderHelper.Less(a[j], a[j - 1], comparator); j--) { OrderHelper.Exch(a, j, j - 1); } Debug.Assert(OrderHelper.IsSorted(a, 0, i, comparator)); } Debug.Assert(OrderHelper.IsSorted(a, comparator)); }
/// <summary> /// Rearranges the array in ascending order, using the natural order.</summary> /// <param name="a">a the array to be sorted</param> /// public static void Sort(IComparable[] a) { int N = a.Length; for (int i = 0; i < N; i++) { for (int j = i; j > 0 && OrderHelper.Less(a[j], a[j - 1]); j--) { OrderHelper.Exch(a, j, j - 1); } Debug.Assert(OrderHelper.IsSorted(a, 0, i)); } Debug.Assert(OrderHelper.IsSorted(a)); }
/// <summary> /// Rearranges the array in ascending order, using the natural order.</summary> /// <param name="a">the array to be sorted</param> /// public static void Sort(IComparable[] a) { int N = a.Length; IComparable[] aux = new IComparable[N]; for (int n = 1; n < N; n = n + n) { for (int i = 0; i < N - n; i += n + n) { int lo = i; int m = i + n - 1; int hi = Math.Min(i + n + n - 1, N - 1); merge(a, aux, lo, m, hi); } } Debug.Assert(OrderHelper.IsSorted(a)); }
/// <summary> /// Rearranges the array in ascending order, using the natural order.</summary> /// <param name="a">a the array to be sorted</param> /// public static void Sort(IComparable[] a) { int N = a.Length; for (int i = 0; i < N; i++) { int min = i; for (int j = i + 1; j < N; j++) { if (OrderHelper.Less(a[j], a[min])) { min = j; } } OrderHelper.Exch(a, i, min); Debug.Assert(OrderHelper.IsSorted(a, 0, i)); } Debug.Assert(OrderHelper.IsSorted(a)); }
/// <summary> /// Rearranges the array in ascending order, using a comparator.</summary> /// <param name="a">a the array</param> /// <param name="c">c the comparator specifying the order</param> /// public static void Sort <T>(T[] a, Comparer <T> c) { int N = a.Length; for (int i = 0; i < N; i++) { int min = i; for (int j = i + 1; j < N; j++) { if (OrderHelper.Less <T>(a[j], a[min], c)) { min = j; } } OrderHelper.Exch(a, i, min); Debug.Assert(OrderHelper.IsSorted(a, 0, i, c)); } Debug.Assert(OrderHelper.IsSorted(a, c)); }
// stably merge a[lo .. mid] with a[mid+1 ..hi] using aux[lo .. hi] private static void merge(IComparable[] a, IComparable[] aux, int lo, int mid, int hi) { // precondition: a[lo .. mid] and a[mid+1 .. hi] are sorted subarrays Debug.Assert(OrderHelper.IsSorted(a, lo, mid)); Debug.Assert(OrderHelper.IsSorted(a, mid + 1, hi)); // copy to aux[] for (int k = lo; k <= hi; k++) { aux[k] = a[k]; } // merge back to a[] int i = lo, j = mid + 1; for (int k = lo; k <= hi; k++) { if (i > mid) { a[k] = aux[j++]; } else if (j > hi) { a[k] = aux[i++]; } else if (OrderHelper.Less(aux[j], aux[i])) { a[k] = aux[j++]; } else { a[k] = aux[i++]; } } // postcondition: a[lo .. hi] is sorted Debug.Assert(OrderHelper.IsSorted(a, lo, hi)); }
// TODO: Add a generic verion /// <summary> /// Rearranges the array in ascending order, using the natural order.</summary> /// <param name="a">a the array to be sorted</param> /// public static void Sort(IComparable[] a) { StdRandom.Shuffle(a); sort(a, 0, a.Length - 1); Debug.Assert(OrderHelper.IsSorted(a)); }