/**
 * Edison Chindrawaly
 * CSCI 4300
 * Files: BubbleSort.h BubbleSort.cc runTest.cc result
 * expl : Replacement Selection Algorithm with BubbleSort
 */
#include "BubbleSort.h"

void look(int,int[]);
void swap(int,int,int[]);

int main()
{
 int NumKeyInRun = 0;
 int RunNumber = 0;
 int RunKeyCount = 0;
 const int total_keys = 2000;
 const int total_workspace  = 100;
 int NumSpace = total_workspace;
 
 int keys[total_keys];
 int run[total_workspace];
 int workspace[total_workspace];

 unsigned int seed;
 srand(seed);
 
 for(int i=0;i<total_keys;i++)
   keys[i] = rand();
 for(int i=0;i<total_workspace;i++)
 {
  run[i] = rand() % 2301;
  workspace[i] = rand() %1024;
 }

 BubbleSort bs(total_workspace);
 cout<<"original unsorted:"<<endl;
 look(total_workspace,workspace); 
 cout<<"-------------------"<<endl;
 bs.Sort(workspace);

 while(RunKeyCount<total_keys)
 {
  if(NumKeyInRun == 0)
  {
   run[0] = workspace[0];
   NumKeyInRun++;
   workspace[0] = keys[RunKeyCount++];
   bs.SortSize(NumSpace,workspace);
  }
  else if(workspace[0]>run[NumKeyInRun-1])
  {
   run[NumKeyInRun++] = workspace[0];
   workspace[0] = keys[RunKeyCount++];
   bs.SortSize(NumSpace,workspace);
  }
  else if(workspace[0]<run[NumKeyInRun-1])
  {
   swap(0,NumSpace-1,workspace);
   NumSpace--;
   bs.SortSize(NumSpace,workspace);
  }
  else // the same number is matching!!
  {
   cout<<"ERR: same number, we are changing it"<<endl;
   workspace[0]=  rand()%1200+12;
  }

  if((RunKeyCount == total_workspace)||(NumSpace==0)||
     (NumKeyInRun==total_workspace))
  {
   cout<<"run number :"<<RunNumber<<endl;
   look(total_workspace,run);
   RunNumber++;
   NumKeyInRun = 0;
   NumSpace = total_workspace;
   bs.Sort(workspace);
   cout<<"-----------------------"<<endl;
  }
 }

 bs.Sort(run);
 cout<<"---------------------------------"<<endl;
 cout<<"Final run: "<<endl;
 look(total_workspace,run);
 cout<<"RunNumber : "<<RunNumber<<endl;
}

void look(int size, int arr[])
{
 int a = 10;
 if(size > 100)
   a = 100;
 
 for(int i=0; i<size/a; i++)
   cout<<i*a<<": "<<arr[i*a]<<endl; 
}

void swap(int a, int b, int array[])
{
 int temp = array[a];
 array[a] = array[b];
 array[b] = temp;
}
 

1