#ifndef CA_UWATERLOO_ALUMNI_DWHARDER_BOGO_SORT_ARRAY #define CA_UWATERLOO_ALUMNI_DWHARDER_BOGO_SORT_ARRAY namespace Sorting_algorithms { namespace Bogo_sort { class Array { public: template static void sort( Type *const array, int const n ) { sort( array, 0, n - 1 ); } template static void sort( Type *const array, int const a, int const b ) { int range = b - a + 1; while ( is_unsorted( array, a, b ) ) { for ( int i = a; i <= b; ++i ) { int j = a + ( rand() % range ); swap( array, i, j ); } } } private: template inline static bool is_unsorted( Type *const array, int const a, int const b ) { for ( int i = a; i < b; ++i ) { if ( array[i] > array[i + 1] ) { return true; } } return false; } template static void swap( Type *const array, int i, int j ) { Type tmp = array[i]; array[i] = array[j]; array[j] = tmp; } }; } } #endif