// L-30 MCS 360 Mon 6 Apr 2020 : selection_sort

/* We sort selecting the smallest element in the sequence
   and swapping that smallest element to the front. */

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

vector< pair<int,char> > random_vector ( int n );
// returns a vector of n random 2-digit numbers
// associated with lower case letters

void write_vector ( vector< pair<int,char> > v );
// writes the vector v

void vanilla_selection_sort ( vector< pair<int,char> >& v );
// applies selection sort to the vector v

int main()
{
   cout << "give n : ";
   int n; cin >> n;

   srand(time(0));
   vector< pair<int,char> > v = random_vector(n);
   cout << n << " random items : ";
   write_vector(v); cout << endl;

   vanilla_selection_sort(v);

   cout << "the sorted vector : ";
   write_vector(v); cout << endl;

   return 0;
}

vector< pair<int,char> > random_vector ( int n )
{
   vector< pair<int,char> > v;

   for(int i=0; i<n; i++)
   {
      pair<int,char> p;
      p.first = 10 + (rand() % 90);
      p.second = 'a' + rand() % 26;
      v.push_back(p);
   }

   return v;
}

void write_vector ( vector< pair<int,char> > v )
{
   for(int i=0; i<v.size(); i++)
      cout << "(" << v[i].first
           << "," << v[i].second
           << ")";
}

void vanilla_selection_sort ( vector< pair<int,char> >& v )
{
   for(int i=0; i<v.size()-1; i++)
   {
      int m = v[i].first;
      int k = i;
      for(int j=i+1; j<v.size(); j++)
         if(v[j].first < m)
         {
            m = v[j].first; k = j;
         }
      if(k != i)
      {
         v[k].first = v[i].first;
         v[i].first = m;
      }
      cout << "after " << k
           << " <-> " << i << " : ";
      write_vector(v); cout << endl;
   }
}
