@@ -43,30 +43,34 @@ namespace qlibs {
4343
4444 /* * @cond */
4545 namespace impl {
46+ struct sort_pair {
47+ int first;
48+ int second;
49+ };
4650 template <typename T, size_t N>
4751 class sort_stack {
4852 private:
4953 T dat[ N ];
5054 size_t topIndex;
5155 public:
5256 sort_stack () : topIndex( 0 ) {}
53- bool empty ( void ) const
57+ bool empty ( void ) const noexcept
5458 {
5559 return 0 == topIndex;
5660 }
57- void push ( const T& value )
61+ void push ( const T& value ) noexcept
5862 {
5963 if ( topIndex < N ) {
6064 dat[ topIndex++ ] = value;
6165 }
6266 }
63- void pop ( void )
67+ void pop ( void ) noexcept
6468 {
6569 if ( topIndex > 0 ) {
6670 --topIndex;
6771 }
6872 }
69- T& top ( void )
73+ T& top ( void ) noexcept
7074 {
7175 return dat[ topIndex - 1 ];
7276 }
@@ -78,7 +82,7 @@ namespace qlibs {
7882 * @brief Sorts the given array in the range [first,last) into ascending
7983 * order.
8084 * @note The elements are compared using operator<
81- * @remark This algorithm uses a non-recursive variant of the quicksort
85+ * @remark This algorithm uses a non-recursive variant of the Quick Sort
8286 * algorithm.
8387 * @param[in,out] array The array to be sorted.
8488 * @param[in] first Initial position of the portion to be sorted
@@ -88,53 +92,51 @@ namespace qlibs {
8892 template <typename T, size_t n>
8993 void sort ( T ( &array )[ n ],
9094 size_t first = 0U,
91- size_t last = n - 1U )
95+ size_t last = n - 1U ) noexcept
9296 {
93- struct pair {
94- int first;
95- int second;
96- };
97- algorithm::impl::sort_stack<pair, n> stack;
98- int start = static_cast <int >( first );
99- int end = static_cast <int >( last );
100-
101- stack.push ( { start, end } );
102- while ( !stack.empty () ) {
103- pair indices = stack.top ();
104- stack.pop ();
105- start = indices.first ;
106- end = indices.second ;
107- int pivotIndex = start;
108- T pivotValue = array[ end ];
109-
110- for ( int i = start; i < end; ++i ) {
111- if ( array[ i ] < pivotValue ) {
112- algorithm::swap ( array[ i ], array[ pivotIndex ] );
113- ++pivotIndex;
97+ if ( n > 1U ) {
98+ algorithm::impl::sort_stack<impl::sort_pair, n> stack;
99+ int start = static_cast <int >( first );
100+ int end = static_cast <int >( last );
101+
102+ stack.push ( { start, end } );
103+ while ( !stack.empty () ) {
104+ impl::sort_pair indices = stack.top ();
105+ stack.pop ();
106+ start = indices.first ;
107+ end = indices.second ;
108+ int pivotIndex = start;
109+ T pivotValue = array[ end ];
110+
111+ for ( int i = start; i < end; ++i ) {
112+ if ( array[ i ] < pivotValue ) {
113+ algorithm::swap ( array[ i ], array[ pivotIndex ] );
114+ ++pivotIndex;
115+ }
116+ }
117+ algorithm::swap ( array[ pivotIndex ], array[ end ] );
118+ if ( pivotIndex - 1 > start ) {
119+ stack.push ( { start, pivotIndex - 1 } );
120+ }
121+ if ( pivotIndex + 1 < end ) {
122+ stack.push ( { pivotIndex + 1 , end } );
114123 }
115- }
116- algorithm::swap ( array[ pivotIndex ], array[ end ] );
117- if ( pivotIndex - 1 > start ) {
118- stack.push ( { start, pivotIndex - 1 } );
119- }
120- if ( pivotIndex + 1 < end ) {
121- stack.push ( { pivotIndex + 1 , end } );
122124 }
123125 }
124126 }
125127
126128 /* *
127129 * @brief Reverses the order of the elements in the range [first,last).
128130 * @param[in,out] array The array to reverse.
129- * @param[in] first Initial position of the portion to reverse
130- * @param[in] last Final position of the portion to reverse
131+ * @param[in] first Initial position of the portion to reverse
132+ * @param[in] last Final position of the portion to reverse
131133 * @return none.
132134 */
133135
134136 template <typename T, size_t n>
135- void reverse ( T ( &array )[ n ],
136- const size_t first = 0U,
137- const size_t last = n - 1U ) noexcept
137+ inline void reverse ( T ( &array )[ n ],
138+ const size_t first = 0U,
139+ const size_t last = n - 1U ) noexcept
138140 {
139141 if ( last > first ) {
140142 size_t s = first, e = last;
0 commit comments