private static int PartitionASC(IComparable[] a, int lo, int hi) { int i = lo, j = hi + 1; IComparable v = a[lo]; while (true) { while (SortHelper.Less(a[++i], v)) { if (i == hi) { break; } } while (SortHelper.Less(v, a[--j])) { if (j == lo) { break; } } if (i >= j) { break; } SortHelper.Exchange(a, i, j); } SortHelper.Exchange(a, lo, j); return(j); }
private static void MergeASC(IComparable[] a, IComparable[] aux, int lo, int mid, int hi) { if (!SortHelper.Less(a[mid + 1], a[mid])) { return; } for (int k = lo; k <= hi; k++) { aux[k] = a[k]; } 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 (SortHelper.Less(aux[j], aux[i])) { a[k] = aux[j++]; } else { a[k] = aux[i++]; } } }
private static int PartitionASC <Key>(Key[] a, IComparer <Key> c, int lo, int hi) where Key : class { int i = lo, j = hi + 1; Key v = a[lo]; while (true) { while (SortHelper.Less(c, a[++i], v)) { if (i == hi) { break; } } while (SortHelper.Less(c, v, a[--j])) { if (j == lo) { break; } } if (i >= j) { break; } SortHelper.Exchange(a, i, j); } SortHelper.Exchange(a, lo, j); return(j); }
private static void MergeASC <Key>(Key[] a, Key[] aux, IComparer <Key> c, int lo, int mid, int hi) where Key : class { if (!SortHelper.Less(c, a[mid + 1], a[mid])) { return; } for (int k = lo; k <= hi; k++) { aux[k] = a[k]; } 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 (SortHelper.Less(c, aux[j], aux[i])) { a[k] = aux[j++]; } else { a[k] = aux[i++]; } } }
/// <summary> /// Rearranges the array in ascending order, using a comparer. /// </summary> /// <param name="a">The array to be sorted</param> /// <param name="c">The comparer that specifying the order</param> private static void SortASC <Key>(Key[] a, IComparer <Key> c) where Key : class { int N = a.Length; for (int i = 0; i < N; i++) { for (int j = i; j > 0 && SortHelper.Less(c, a[j], a[j - 1]); j--) { SortHelper.Exchange(a, j, j - 1); } } }
/// <summary> /// Rearranges the array in ascending order, using the natural order. /// </summary> /// <param name="a">The array to be sorted</param> private static void SortASC(IComparable[] a) { int N = a.Length; for (int i = 0; i < N; i++) { for (int j = i; j > 0 && SortHelper.Less(a[j], a[j - 1]); j--) { SortHelper.Exchange(a, j, j - 1); } } }
/// <summary> /// Rearranges the array in ascending order, using the natural order. /// </summary> /// <param name="a">The array to be sorted</param> private static void SortASC(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 (SortHelper.Less(a[j], a[min])) { min = j; } } SortHelper.Exchange(a, min, i); } }
/// <summary> /// Rearranges the array in ascending order, using a comparer. /// </summary> /// <param name="a">The array to be sorted</param> /// <param name="c">the comparer that specifying the order</param> private static void SortASC <Key>(Key[] a, IComparer <Key> c) where Key : class { int N = a.Length; for (int i = 0; i < N; i++) { int min = i; for (int j = i + 1; j < N; j++) { if (SortHelper.Less(c, a[j], a[min])) { min = j; } } SortHelper.Exchange(a, i, min); } }
/// <summary> /// Rearranges the array in ascending order, using the natural order. /// </summary> /// <param name="a">The array to be sorted</param> private static void SortASC(IComparable[] a) { int N = a.Length; int h = 1; while (h < N / 3) { h = h * 3 + 1; } while (h >= 1) { for (int i = h; i < N; i++) { for (int j = i; j >= h && SortHelper.Less(a[j], a[j - h]); j -= h) { SortHelper.Exchange(a, j, j - h); } } h /= 3; } }
/// <summary> /// Rearranges the array in ascending order, using a comparer. /// </summary> /// <param name="a">The array to be sorted</param> /// <param name="c">The comparer that specifying the order</param> private static void SortASC <Key>(Key[] a, IComparer <Key> c) where Key : class { int N = a.Length; int h = 1; while (h < N / 3) { h = h * 3 + 1; } while (h >= 1) { for (int i = h; i < N; i++) { for (int j = i; j >= h && SortHelper.Less(c, a[j], a[j - h]); j -= h) { SortHelper.Exchange(a, j, j - h); } } h /= 3; } }
private static bool Less <Key>(Key[] pq, IComparer <Key> c, int i, int j) where Key : class { return(SortHelper.Less <Key>(c, pq[i - 1], pq[j - 1])); }
private static bool Less(IComparable[] pq, int i, int j) { return(SortHelper.Less(pq[i - 1], pq[j - 1])); }