// L-12 MCS 572 Mon 6 Feb 2012 : time_stl_sort

// We use the STL sort on a vector of random doubles.
// For timing purposes, the dimension and verbose level
// can be entered at the command line.

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <algorithm>
using namespace std;

vector<double> random_vector ( int n );
// returns a vector of n random doubles

void write_vector ( vector<double> v );
// writes the vector v

struct less_than // defines "<"
{
   bool operator()(const double& a,
                   const double& b)
   {
      return (a < b);
   }
};

int main ( int argc, char *argv[] )
{
   int n;         // n is dimension
   if(argc > 1)
      n = atoi(argv[1]);
   else
   {
      cout << "give n : ";
      cin >> n;
   }
   int vb = 1;
   if(argc > 2)
      vb = atoi(argv[2]);
   srand(time(0)); // random seed
   vector<double> v = random_vector(n);
   if(vb > 0)
   {
      cout << n << " random doubles :\n";
      write_vector(v);
   }
   clock_t tstart,tstop;
   tstart = clock();
   sort(v.begin(),v.end(),less_than());
   tstop = clock();
   cout << "time elapsed : " 
        << (tstop - tstart)/double(CLOCKS_PER_SEC) 
        << " seconds" << endl;
   if(vb > 0)
   {
      cout << "the sorted vector : \n";
      write_vector(v);
   }
   return 0;
}

vector<double> random_vector ( int n )
{
   vector<double> v;

   for(int i=0; i<n; i++)
   {
      double r = (double) rand();
      r = r/RAND_MAX;
      v.push_back(r);
   }
   return v;
}

void write_vector ( vector<double> v )
{
   for(int i=0; i<v.size(); i++)
      cout << scientific 
           << setprecision(15)
           << v[i] << endl;
}

