
Patrick B. answered 06/23/20
Math and computer tutor/teacher
it is O(N*logN)
How many records are in the array?
suppose you have 1000 records...
then 1000 * log 1000 = 1000*3 = 3000 ms
it will sort the array is 3000 ms = 3 secs
Kojo K.
asked 06/22/20This is the implementation.
template <typename Comparable>
void SORT( vector<Comparable> & items )
{
if( items.size( ) > 1 )
{
vector<Comparable> smaller;
vector<Comparable> same;
vector<Comparable> larger;
auto chosenItem = items[ items.size( ) / 2 ];
for( auto & i : items )
{
if( i < chosenItem )
smaller.push_back( std::move( i ) );
else if( chosenItem < i )
larger.push_back( std::move( i ) );
else
same.push_back( std::move( i ) );
}
SORT( smaller ); // Recursive call!
SORT( larger ); // Recursive call!
std::move( begin( smaller ), end( smaller ), begin( items ) );
std::move( begin( same ), end( same ), begin( items ) + smaller.size( ) );
std::move( begin( larger ), end( larger ), end( items ) - larger.size( ) );
}
}
Patrick B. answered 06/23/20
Math and computer tutor/teacher
it is O(N*logN)
How many records are in the array?
suppose you have 1000 records...
then 1000 * log 1000 = 1000*3 = 3000 ms
it will sort the array is 3000 ms = 3 secs
Get a free answer to a quick problem.
Most questions answered within 4 hours.
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Kojo K.
Is it O(N*logN) even when the input is a random input? I was thinking it will be a worst case but I am not sure.06/23/20