X-Git-Url: https://feistymeow.org/gitweb/?a=blobdiff_plain;f=nucleus%2Flibrary%2Falgorithms%2Fsorts.h;h=c6e21abedc66ec69992016668e7e1ec51685db14;hb=7d0b5833568389c06ff6d9871da343a1e3e374fe;hp=052ecb24419bf262326115193d08acc0e4c78609;hpb=244e0eeaf916718d7bcd7beba44f9c0de29c504d;p=feisty_meow.git diff --git a/nucleus/library/algorithms/sorts.h b/nucleus/library/algorithms/sorts.h index 052ecb24..c6e21abe 100644 --- a/nucleus/library/algorithms/sorts.h +++ b/nucleus/library/algorithms/sorts.h @@ -16,6 +16,8 @@ // Please send updates for this code to: fred@gruntose.com -- Thanks, fred. ////////////// +#include + namespace algorithms { /* @@ -264,9 +266,9 @@ namespace algorithms { } private: - bool _reverse = false; // is the sorting in reverse? - int _total = 0; - int *_heapspace = NULL_POINTER; + bool _reverse; // is the sorting in reverse? + int _total; // how many total elements are there? + int *_heapspace; // track a pointer to the array. }; /*! @@ -345,12 +347,27 @@ namespace algorithms { * operates in O(n log(n)) time on average, worst case O(n^2). * sorts the original array. */ - template + template void quick_sort(type v[], int n, bool reverse = false) { inner_quick_sort(v, 0, n - 1, reverse); } + ////////////// + + //! handy method for randomizing the order of a list. not strictly a sorting function... + template + void randomize_list(type v[], int n) + { + mathematics::chaos randomizer; + for (int i = 0; i < n; i++) { + // we will swap with any element that is not prior to the current index; thus we allow + // swapping the element with itself and later, but not with anything earlier. + int swap_index = randomizer.inclusive(i, n - 1); + swap_values(v, i, swap_index); + } + } + } // namespace. #endif // outer guard.