/// <summary> This is a generic version of C.A.R Hoare's Quick Sort algorithm. /// This will handle Sortable objects that are already /// sorted, and Sortable objects with duplicate keys. /// <p> /// </summary> /// <param name="sortable">A <code>Sortable</code> object. /// </param> /// <param name="lo0">Left boundary of partition. /// </param> /// <param name="hi0">Right boundary of partition. /// </param> public static void QuickSort(ISortable sortable, int lo0, int hi0) { int lo = lo0; int hi = hi0; IOrdered mid; IOrdered test; if (hi0 > lo0) { // arbitrarily establish partition element as the midpoint of the vector mid = sortable.Fetch((lo0 + hi0) / 2, null); test = null; // loop through the vector until indices cross while (lo <= hi) { // find the first element that is greater than or equal to // the partition element starting from the left index while ((lo < hi0) && (0 > (test = sortable.Fetch(lo, test)).Compare(mid))) { ++lo; } // find an element that is smaller than or equal to // the partition element starting from the right index while ((hi > lo0) && (0 < (test = sortable.Fetch(hi, test)).Compare(mid))) { --hi; } // if the indexes have not crossed, swap if (lo <= hi) { sortable.Swap(lo++, hi--); } } // if the right index has not reached the left side of array // must now sort the left partition if (lo0 < hi) { QuickSort(sortable, lo0, hi); } // if the left index has not reached the right side of array // must now sort the right partition if (lo < hi0) { QuickSort(sortable, lo, hi0); } } }
/// <summary> Binary search for an object</summary> /// <param name="set_Renamed">The collection of <code>Ordered</code> objects. /// </param> /// <param name="ref_Renamed">The name to search for. /// </param> /// <param name="lo">The lower index within which to look. /// </param> /// <param name="hi">The upper index within which to look. /// </param> /// <returns> The index at which reference was found or is to be inserted. /// </returns> public static int Bsearch(ISortable set_Renamed, IOrdered ref_Renamed, int lo, int hi) { int num; int mid; IOrdered ordered; int half; int result; int ret; ret = -1; num = (hi - lo) + 1; ordered = null; while ((-1 == ret) && (lo <= hi)) { half = num / 2; mid = lo + ((0 != (num & 1))?half:half - 1); ordered = set_Renamed.Fetch(mid, ordered); result = ref_Renamed.Compare(ordered); if (0 == result) { ret = mid; } else if (0 > result) { hi = mid - 1; num = ((0 != (num & 1))?half:half - 1); } else { lo = mid + 1; num = half; } } if (-1 == ret) { ret = lo; } return(ret); }
/// <summary> Binary search for an object</summary> /// <param name="set_Renamed">The collection of <code>Ordered</code> objects. /// </param> /// <param name="ref_Renamed">The name to search for. /// </param> /// <param name="lo">The lower index within which to look. /// </param> /// <param name="hi">The upper index within which to look. /// </param> /// <returns> The index at which reference was found or is to be inserted. /// </returns> public static int Bsearch(ISortable set_Renamed, IOrdered ref_Renamed, int lo, int hi) { int num; int mid; IOrdered ordered; int half; int result; int ret; ret = - 1; num = (hi - lo) + 1; ordered = null; while ((- 1 == ret) && (lo <= hi)) { half = num / 2; mid = lo + ((0 != (num & 1))?half:half - 1); ordered = set_Renamed.Fetch(mid, ordered); result = ref_Renamed.Compare(ordered); if (0 == result) ret = mid; else if (0 > result) { hi = mid - 1; num = ((0 != (num & 1))?half:half - 1); } else { lo = mid + 1; num = half; } } if (- 1 == ret) ret = lo; return (ret); }
/// <summary> This is a generic version of C.A.R Hoare's Quick Sort algorithm. /// This will handle Sortable objects that are already /// sorted, and Sortable objects with duplicate keys. /// <p> /// </summary> /// <param name="sortable">A <code>Sortable</code> object. /// </param> /// <param name="lo0">Left boundary of partition. /// </param> /// <param name="hi0">Right boundary of partition. /// </param> public static void QuickSort(ISortable sortable, int lo0, int hi0) { int lo = lo0; int hi = hi0; IOrdered mid; IOrdered test; if (hi0 > lo0) { // arbitrarily establish partition element as the midpoint of the vector mid = sortable.Fetch((lo0 + hi0) / 2, null); test = null; // loop through the vector until indices cross while (lo <= hi) { // find the first element that is greater than or equal to // the partition element starting from the left index while ((lo < hi0) && (0 > (test = sortable.Fetch(lo, test)).Compare(mid))) ++lo; // find an element that is smaller than or equal to // the partition element starting from the right index while ((hi > lo0) && (0 < (test = sortable.Fetch(hi, test)).Compare(mid))) --hi; // if the indexes have not crossed, swap if (lo <= hi) sortable.Swap(lo++, hi--); } // if the right index has not reached the left side of array // must now sort the left partition if (lo0 < hi) QuickSort(sortable, lo0, hi); // if the left index has not reached the right side of array // must now sort the right partition if (lo < hi0) QuickSort(sortable, lo, hi0); } }