Skip to content

Commit

Permalink
some refactor to algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
camilo committed Feb 20, 2024
1 parent a072f03 commit 0aa8af5
Showing 1 changed file with 41 additions and 39 deletions.
80 changes: 41 additions & 39 deletions src/include/algorithm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,34 @@ namespace qlibs {

/** @cond */
namespace impl {
struct sort_pair {
int first;
int second;
};
template<typename T, size_t N>
class sort_stack {
private:
T dat[ N ];
size_t topIndex;
public:
sort_stack() : topIndex( 0 ) {}
bool empty( void ) const
bool empty( void ) const noexcept
{
return 0 == topIndex;
}
void push( const T& value )
void push( const T& value ) noexcept
{
if ( topIndex < N ) {
dat[ topIndex++ ] = value;
}
}
void pop( void )
void pop( void ) noexcept
{
if ( topIndex > 0 ) {
--topIndex;
}
}
T& top( void )
T& top( void ) noexcept
{
return dat[ topIndex - 1 ];
}
Expand All @@ -78,7 +82,7 @@ namespace qlibs {
* @brief Sorts the given array in the range [first,last) into ascending
* order.
* @note The elements are compared using operator<
* @remark This algorithm uses a non-recursive variant of the quicksort
* @remark This algorithm uses a non-recursive variant of the Quick Sort
* algorithm.
* @param[in,out] array The array to be sorted.
* @param[in] first Initial position of the portion to be sorted
Expand All @@ -88,53 +92,51 @@ namespace qlibs {
template<typename T, size_t n>
void sort( T ( &array )[ n ],
size_t first = 0U,
size_t last = n - 1U )
size_t last = n - 1U ) noexcept
{
struct pair {
int first;
int second;
};
algorithm::impl::sort_stack<pair, n> stack;
int start = static_cast<int>( first );
int end = static_cast<int>( last );

stack.push( { start, end } );
while ( !stack.empty() ) {
pair indices = stack.top();
stack.pop();
start = indices.first;
end = indices.second;
int pivotIndex = start;
T pivotValue = array[ end ];

for ( int i = start; i < end; ++i ) {
if ( array[ i ] < pivotValue ) {
algorithm::swap( array[ i ], array[ pivotIndex ] );
++pivotIndex;
if ( n > 1U ) {
algorithm::impl::sort_stack<impl::sort_pair, n> stack;
int start = static_cast<int>( first );
int end = static_cast<int>( last );

stack.push( { start, end } );
while ( !stack.empty() ) {
impl::sort_pair indices = stack.top();
stack.pop();
start = indices.first;
end = indices.second;
int pivotIndex = start;
T pivotValue = array[ end ];

for ( int i = start; i < end; ++i ) {
if ( array[ i ] < pivotValue ) {
algorithm::swap( array[ i ], array[ pivotIndex ] );
++pivotIndex;
}
}
algorithm::swap( array[ pivotIndex ], array[ end ] );
if ( pivotIndex - 1 > start ) {
stack.push( { start, pivotIndex - 1 } );
}
if ( pivotIndex + 1 < end ) {
stack.push( { pivotIndex + 1, end } );
}
}
algorithm::swap( array[ pivotIndex ], array[ end ] );
if ( pivotIndex - 1 > start ) {
stack.push( { start, pivotIndex - 1 } );
}
if ( pivotIndex + 1 < end ) {
stack.push( { pivotIndex + 1, end } );
}
}
}

/**
* @brief Reverses the order of the elements in the range [first,last).
* @param[in,out] array The array to reverse.
* @param[in] first Initial position of the portion to reverse
* @param[in] last Final position of the portion to reverse
* @param[in] first Initial position of the portion to reverse
* @param[in] last Final position of the portion to reverse
* @return none.
*/

template<typename T, size_t n>
void reverse( T ( &array )[ n ],
const size_t first = 0U,
const size_t last = n - 1U ) noexcept
inline void reverse( T ( &array )[ n ],
const size_t first = 0U,
const size_t last = n - 1U ) noexcept
{
if ( last > first ) {
size_t s = first, e = last;
Expand Down

0 comments on commit 0aa8af5

Please sign in to comment.