/// <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> 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); } }