/* sortdriver2.cpp is used to Time various sorting algorithms
 *
 * Written by:   Larry R. Nyhoff
 * Written for:  Lab Manual for C++: An Introduction to Data Structures
 *
 * Add other documentation required by your instructor such as your name, 
 * course number, and the current date.
 *************************************************************************/

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

#include "SortLibrary"
#include "Timer.h"

int main()
{
  const char SORTMENU[] =
    "0  TO STOP\n"
    "1  Bubble sort\n"
    "2  Simple selection sort\n"
    "3  Linear Insertion sort\n"
    "4  Quicksort\n"
    "5  STL's sort\n";

  const string SORTNAME[] = {"Bubble", "Simple selection ", 
                             "Linear insertion ","Quick", "STL's "};

  string fileName;
  cout << "Enter name of file to sort: ";
  cin >> fileName;
  
  ifstream inFile;
  inFile.open(fileName.c_str());
  if (!inFile.is_open())
  {
    cerr << "\nError: Unable to open the file " << fileName << endl;
    exit(-1);
  }

  // Read vector orig with values from inFile
  vector<string> orig;
  string str;
  for (;;)
  {
    inFile >> str;
    if (inFile.eof()) break;
    orig.push_back(str);
  }

  cout << "We will be sorting a list of " << orig.size() << " strings\n\n"
          "Here are the sorting methods available:\n" << SORTMENU << endl;

  int method;
  for (;;)
  {
    cout << "\nEnter number of sorting scheme to use: ";
    cin >> method;
    if (method == 0) break;

    // Reset vector x back to original
    vector<string> x = orig;

    // Perform the sort and time it
    Timer t;
    switch (method)
    {
      case 1: t.Start(); BubbleSort(x); t.Stop();
              break;
      case 2: t.Start(); SelectionSort(x); t.Stop();
              break;
      case 3: t.Start(); InsertionSort(x); t.Stop();
              break;
      case 4: t.Start(); Quickort(x, 1, x.size() - 1); t.Stop();
              break;
      case 5: t.Start(); sort(x.begin() + 1, x.end()); t.Stop();
              break;
      default:
              cerr << "*** Bad sorting method " << method << endl;
    }

    // Display the time.
    cout << "*** " << SORTNAME[method-1] << "sort took: ";
         << t.Seconds() << " seconds." << endl;
  }
}

