/* ----------------------------------------------------------------------------- GSFramework Copyright 2001-2013 Emmanuel Julien. All Rights Reserved. ----------------------------------------------------------------------------- */ #ifndef __CONTAINER_SORT__ #define __CONTAINER_SORT__ namespace GS { class ContainerSort { template static inline void Swap(L &t, int i, int j) { if (i != j) { T swap = t[j]; t[j] = t[i]; t[i] = swap; } } template static int QuickSortPartition(L &t, C compare, int first, int last, int pivot) { Swap (t, pivot, last); int j = first; for (int i = first; i < last; ++i) if (compare(t[i], t[last]) > 0) { Swap (t, i, j); ++j; } Swap (t, j, last); return j; } template static void QuickSortStep(L &t, C compare, int first, int last) { if (first < last) { int pivot = (first + last) / 2; pivot = QuickSortPartition (t, compare, first, last, pivot); QuickSortStep (t, compare, first, pivot - 1); QuickSortStep (t, compare, pivot + 1, last); } } public: /// Quick-sort a container in-place. template static void QuickSort(L &t, C compare) { QuickSortStep (t, compare, 0, t.GetCount() - 1); } }; } // GS #endif // __CONTAINER_SORT__