@@ -43,30 +43,34 @@ namespace qlibs {
43
43
44
44
/* * @cond */
45
45
namespace impl {
46
+ struct sort_pair {
47
+ int first;
48
+ int second;
49
+ };
46
50
template <typename T, size_t N>
47
51
class sort_stack {
48
52
private:
49
53
T dat[ N ];
50
54
size_t topIndex;
51
55
public:
52
56
sort_stack () : topIndex( 0 ) {}
53
- bool empty ( void ) const
57
+ bool empty ( void ) const noexcept
54
58
{
55
59
return 0 == topIndex;
56
60
}
57
- void push ( const T& value )
61
+ void push ( const T& value ) noexcept
58
62
{
59
63
if ( topIndex < N ) {
60
64
dat[ topIndex++ ] = value;
61
65
}
62
66
}
63
- void pop ( void )
67
+ void pop ( void ) noexcept
64
68
{
65
69
if ( topIndex > 0 ) {
66
70
--topIndex;
67
71
}
68
72
}
69
- T& top ( void )
73
+ T& top ( void ) noexcept
70
74
{
71
75
return dat[ topIndex - 1 ];
72
76
}
@@ -78,7 +82,7 @@ namespace qlibs {
78
82
* @brief Sorts the given array in the range [first,last) into ascending
79
83
* order.
80
84
* @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
82
86
* algorithm.
83
87
* @param[in,out] array The array to be sorted.
84
88
* @param[in] first Initial position of the portion to be sorted
@@ -88,53 +92,51 @@ namespace qlibs {
88
92
template <typename T, size_t n>
89
93
void sort ( T ( &array )[ n ],
90
94
size_t first = 0U,
91
- size_t last = n - 1U )
95
+ size_t last = n - 1U ) noexcept
92
96
{
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 } );
114
123
}
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 } );
122
124
}
123
125
}
124
126
}
125
127
126
128
/* *
127
129
* @brief Reverses the order of the elements in the range [first,last).
128
130
* @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
131
133
* @return none.
132
134
*/
133
135
134
136
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
138
140
{
139
141
if ( last > first ) {
140
142
size_t s = first, e = last;
0 commit comments