91
C/C++ & Visual C++ / Re: Lists ... examples of steps to a SLL (Single-Link Linked List) and a DLL ...
« Last post by David on February 12, 2017, 11:27:24 PM »Now a timed test program so that you can easily compare sorting times ...
Code: [Select]
// 6sorts_timed_all_auto_version.cpp // // revised: 2017-02-13 //
#include "DLLinsertSorted.h"
// include <iostream> // included above //
#include <string>
#include <cstdlib> // re, rand, srand
#include <ctime> // re, time clock
#include <climits> // re. RAND_MAX
#include <cctype> // re. tolower
#include <cmath> // re. log
#include <list> // to compare c++ library 'list sort' times with 'list sort' times //
const int MAX_PRINT = 11;
const int MAX_NxN = 30000;
// 3 utilities to ease coding here ...
template< typename T >
T takeIn( const std::string& msg, const std::string& errMsg = "\nError! Try again.\n\n" )
{
T val = T();
while( true )
{
std::cout << msg;
if( std::cin >> val && std::cin.get() == '\n' )
break;
else
{
std::cout << errMsg;
std::cin.clear(); // clear error flasgs
std::cin.sync(); // 'flush' cin stream ...
}
}
return val;
}
char takeInChr( const std::string& msg )
{
std::cout << msg;
std::string reply;
getline( std::cin, reply );
if( reply.size() )
return reply[0];
// else ...
return 0;
}
bool more( const std::string& text = "" )
{
char reply = takeInChr( "More " + text + "(y/n) ? " );
if( reply == 'n' || reply == 'N' )
return false;
// else ...
return true;
}
// get a List of random ints in range 0..MAX_RAND
void fillUpList( List< int >& lst, int num )
{
std::cout << "RAND_MIN = " << 0 << ", RAND_MAX = " << RAND_MAX << '\n';
for( int i = 0; i < num; ++ i )
lst.push_back( rand() );
}
template < typename T >
std::ostream& operator << ( std::ostream& os, const List< T >& lst )
{
typename List< T >::const_iterator it;
for( it = lst.begin(); it != lst.end(); ++ it ) os << *it << ' ';
return os;
}
int main()
{
using std::cout; using std::cin;
using std::string;
srand( time(0) ); // seed the random generator //
do
{
int num = 0;
for( ; ; )
{
num = takeIn< int >( "How many numbers to sort (10..2000000): " );
if( num >= 10 && num <= 2000000 ) break;
//else ///
cout << "\nTry again with a value in range 10..2000000 ...\n\n";
}
List< int > lstCpy;
fillUpList( lstCpy, num );
List< int > lst;
List< int >::const_iterator cit;
double dt, t1, k;
if( num <= MAX_NxN )
{
lst = lstCpy; // get from a copy //
t1 = clock();
lst.insert_sort();
dt = (clock() - t1)/CLOCKS_PER_SEC;
cout << "\nTime to insert_sort was " << dt << " seconds "
<< ( lst.isSorted() ? "and SORTED ok." : "but was NOT sorted!" );
// t = k*n*n;
// k = t/n/n
k = dt/num/num;
lst = lstCpy;
for( cit = lstCpy.begin(); cit != lstCpy.end(); ++ cit )
lst.push_back( *cit );
// Now size is doubled //
t1 = clock();
lst.insert_sort();
dt = (clock() - t1)/CLOCKS_PER_SEC;
cout << "\nTime to insert_sort was " << dt << " seconds "
<< ( lst.isSorted() ? "and SORTED ok." : "but was NOT sorted!" );
cout << " Expected: " << 4*k*num*num << '\n';
lst.clear();
t1 = clock();
for( cit = lstCpy.begin(); cit != lstCpy.end(); ++ cit )
lst.insert_sorted( *cit );
dt = (clock() - t1)/CLOCKS_PER_SEC;
cout << "\nTime to insert_sorted was " << dt << " seconds "
<< ( lst.isSorted() ? "and SORTED ok." : "but was NOT sorted!" );
k = dt/num/num;
lst.clear();
for( cit = lstCpy.begin(); cit != lstCpy.end(); ++ cit )
lst.insert_sorted( *cit );
for( cit = lstCpy.begin(); cit != lstCpy.end(); cit ++ ) // test POST ++ //
lst.insert_sorted( *cit );
// Now size is doubled //
dt = (clock() - t1)/CLOCKS_PER_SEC;
cout << "\nTime to insert_sorted was " << dt << " seconds "
<< ( lst.isSorted() ? "and SORTED ok." : "but was NOT sorted!" );
cout << " Expected: " << 4*k*num*num << '\n';
lst = lstCpy; // restore from copy //
t1 = clock();
lst.select_sort();
dt = (clock() - t1)/CLOCKS_PER_SEC;
cout << "\nTime to select_sort was " << dt << " seconds "
<< ( lst.isSorted() ? "and SORTED ok." : "but was NOT sorted!" );
k = dt/num/num;
lst = lstCpy; // restore from copy //
for( cit = lstCpy.begin(); cit != lstCpy.end(); ++ cit )
lst.push_back( *cit );
// Now size is doubled //
t1 = clock();
lst.select_sort();
dt = (clock() - t1)/CLOCKS_PER_SEC;
cout << "\nTime to select_sort was " << dt << " seconds "
<< ( lst.isSorted() ? "and SORTED ok." : "but was NOT sorted!" );
cout << " Expected: " << 4*k*num*num << '\n';
lst = lstCpy; // restore from copy //
t1 = clock();
lst.bubble_sort();
dt = (clock() - t1)/CLOCKS_PER_SEC;
cout << "\nTime to bubble_sort was " << dt << " seconds "
<< ( lst.isSorted() ? "and SORTED ok." : "but was NOT sorted!" );
k = dt/num/num;
lst = lstCpy; // restore from copy //
for( cit = lstCpy.begin(); cit != lstCpy.end(); ++ cit )
lst.push_back( *cit );
// Now size is doubled //
t1 = clock();
lst.bubble_sort();
dt = (clock() - t1)/CLOCKS_PER_SEC;
cout << "\nTime to bubble_sort was " << dt << " seconds "
<< ( lst.isSorted() ? "and SORTED ok." : "but was NOT sorted!" );
cout << " Expected: " << 4*k*num*num << '\n';
}
lst = lstCpy; // restore from copy //
if( num <= MAX_PRINT ) cout << "\nUnsorted: " << lst << '\n';
t1 = clock();
lst.merge_sort();
dt = (clock() - t1)/CLOCKS_PER_SEC;
cout << "\nTime to merge_sort was " << dt<< " seconds "
<< ( lst.isSorted() ? "and SORTED ok." : "but was NOT sorted!" );
if( num <= MAX_PRINT )
{
cout << "\nSorted : " << lst << "\nReversed: ";
lst.printReversed();
cout << '\n';
}
// t = k*n*log(n)
// k = t/n*/log(n)
k = dt/num/log(num);
lst = lstCpy; // restore from copy //
for( cit = lstCpy.begin(); cit != lstCpy.end(); ++ cit )
lst.push_back( *cit );
// Now size is doubled //
t1 = clock();
lst.merge_sort();
dt = (clock() - t1)/CLOCKS_PER_SEC;
cout << "\nTime to merge_sort was " << dt << " seconds "
<< ( lst.isSorted() ? "and SORTED ok." : "but was NOT sorted!" );
cout << " Expected: " << k*2*num*log(2*num) << '\n';
// compare with C++ library list ...
std::list< int > cppLst;
for( cit = lstCpy.begin(); cit != lstCpy.end(); ++ cit )
cppLst.push_back( *cit );
t1 = clock();
cppLst.sort();
dt = (clock() - t1)/CLOCKS_PER_SEC;
cout << "\nC++ library list sort was " << dt << " seconds.";
k = dt/num/log(num);
cppLst.clear();
for( cit = lstCpy.begin(); cit != lstCpy.end(); ++ cit )
cppLst.push_back( *cit );
for( cit = lstCpy.begin(); cit != lstCpy.end(); ++ cit )
cppLst.push_back( *cit );
// Now size is doubled //
t1 = clock();
cppLst.sort();
dt = (clock() - t1)/CLOCKS_PER_SEC;
cout << "\nC++ library list sort was " << dt << " seconds.";
cout << " Expected: " << k*2*num*log(2*num) << '\n';
cout << '\n';
}
while( more() );
}