public static void Sort <T>(T[] a, Comparison <T> compare) { var n = a.Length; var h = 1; while (h < n / 3) { h = 3 * h + 1; } var step = h; while (step > 0) { for (var i = step; i < n; i++) { for (var j = i; j >= step; j -= step) { if (SortUtil.IsLessThan(a[j], a[j - step], compare)) { SortUtil.Exchange(a, j, j - step); } else { break; } } } step--; } }
public static int Partition <T>(T[] a, int lo, int hi, Comparison <T> compareTo) { int i = lo; int j = hi + 1; while (true) { while (SortUtil.IsLessThan(a[++i], a[lo], compareTo)) { if (i == hi) { break; } } while (SortUtil.IsLessThan(a[lo], a[--j], compareTo)) { if (j == lo) { break; } } if (i >= j) { break; } SortUtil.Exchange(a, i, j); } SortUtil.Exchange(a, lo, j); return(j); }
private static void Sort(string[] a, int lo, int hi, int d) { if (lo >= hi) { return; } var c = charAt(a[lo], d); int i = lo, lt = lo, gt = hi; while (i <= gt) { var cmp = c.CompareTo(charAt(a[i], d)); if (cmp > 0) { SortUtil.Exchange(a, i++, lt++); } else if (cmp < 0) { SortUtil.Exchange(a, i, gt--); } else { i++; } } Sort(a, lo, lt - 1, d); if (c >= 0) { Sort(a, lt, gt, d + 1); } Sort(a, gt + 1, hi, d); }
public static void Shuffle <T>(T[] a) { var random = new Random(); for (var i = 1; i < a.Length; ++i) { var j = random.Next(i + 1); SortUtil.Exchange(a, i, j); } }
public int DelMin() { var item = pq[1]; SortUtil.Exchange(pq, 1, N); qp[pq[1]] = 1; qp[pq[N]] = N; N--; Sink(1); return(item); }
public QueueEnumerator(T[] s, int N) { s = (T[])s.Clone(); this.values = new T[N]; int k = 0; while (N > 0) { values[k++] = s[1]; SortUtil.Exchange(s, 1, N--); Sink(s, 1, N); } }
public static void Sort <T>(T[] a, Comparison <T> comparison) { var N = a.Length; for (var i = N / 2; i >= 1; --i) { Sink(a, i, N, comparison); } while (N > 0) { SortUtil.Exchange(a, Index(1), Index(N)); Sink(a, 1, --N, comparison); } }
public static void Sort <T>(T[] a, int lo, int hi, Comparison <T> comparator) { for (var i = lo; i < hi; ++i) { var max = a[i]; var J = i; for (var j = i + 1; j <= hi; ++j) { if (SortUtil.IsLessThan(a[j], a[J], comparator)) { J = j; } } SortUtil.Exchange(a, i, J); } }
private void Swim(int k) { while (k > 1) { var parent = k / 2; if (SortUtil.IsGreaterThan(s[k], s[parent])) { SortUtil.Exchange(s, k, parent); k = parent; } else { break; } } }
public T DelMax() { if (IsEmpty) { return(default(T)); } var item = s[1]; SortUtil.Exchange(s, 1, N--); Sink(1); if (N < s.Length / 4) { Resize(s.Length / 2); } return(item); }
public static void Sort <T>(T[] a, int lo, int hi, Comparison <T> compare) { for (var i = lo + 1; i <= hi; ++i) { for (var j = i - 1; j >= lo; --j) { if (SortUtil.IsLessThan(a[j + 1], a[j], compare)) { SortUtil.Exchange(a, j, j + 1); } else { break; } } } }
private static void Sink <T>(T[] a, int k, int N, Comparison <T> comparison) { while (k * 2 <= N) { var child = 2 * k; if (child < N && SortUtil.IsGreaterThan(a[Index(child + 1)], a[Index(child)], comparison)) { child++; } if (SortUtil.IsGreaterThan(a[Index(child)], a[Index(k)], comparison)) { SortUtil.Exchange(a, Index(child), Index(k)); k = child; } else { break; } } }
private void Sink(int k) { while (2 * k < N) { int child = k * 2; if (child < N && SortUtil.IsGreaterThan(s[child + 1], s[child])) { child++; } if (SortUtil.IsGreaterThan(s[child], s[k])) { SortUtil.Exchange(s, child, k); k = child; } else { break; } } }
private void Sink(T[] s, int k, int N) { while (k * 2 <= N) { int child = k * 2; if (child < N && SortUtil.IsGreaterThan(s[child + 1], s[child])) { child++; } if (SortUtil.IsGreaterThan(s[child], s[k])) { SortUtil.Exchange(s, k, child); k = child; } else { break; } } }
private void Swim(int k) { while (k > 1) { var parent = k / 2; if (SortUtil.IsLessThan(keys[pq[k]], keys[pq[parent]])) { SortUtil.Exchange(pq, k, parent); qp[pq[k]] = k; qp[pq[parent]] = parent; k = parent; } else { break; } } }
private void Sink(int k) { while (k * 2 <= N) { int child = 2 * k; if (child < N && SortUtil.IsLessThan(keys[pq[child + 1]], keys[pq[child]])) { child++; } if (SortUtil.IsLessThan(keys[pq[child]], keys[pq[k]])) { SortUtil.Exchange(pq, child, k); qp[pq[child]] = child; qp[pq[k]] = k; k = child; } else { break; } } }
public static void Sort <T>(T[] a, int lo, int hi, Comparison <T> compare) { if (lo >= hi) { return; } if (hi - lo < 7) { InsertionSort.Sort(a, lo, hi, compare); return; } int i = lo, lt = lo, gt = hi; var v = a[lo]; while (i <= gt) { if (SortUtil.IsLessThan(a[i], v, compare)) { SortUtil.Exchange(a, i++, lt++); } else if (SortUtil.IsLessThan(v, a[i], compare)) { SortUtil.Exchange(a, i, gt--); } else { i++; } } Sort(a, lo, lt - 1, compare); Sort(a, gt + 1, hi, compare); }