#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>

int swaps = 0;
int comparisons = 0;

void quickSort(int* first, int* last);

void bubbleSort(int a[], int size );

void arrayDisplay( char* title, int A[], int size ) {
  cout << title ;
  for ( int i = 0; i < size; i++ ) {
    if ( 0 == i%10 ) cout << endl;
    cout << setw( 6 ) << A[ i ];
  }
  cout << endl;
}

int main( int argc, char* argv[] ) {

  if ( argc < 2 ) {
    cerr << "Usage: " << argv[0] <<  " arraysize method [noprint]" << endl;
    cerr << "To see a random array of size elements sorted by indicated method"
	 << endl;
    cerr << "(q for quicksort, b for bubblesort)" 
	 << endl;
    cerr << "a third argument, if present, suppresses printing" << endl;
    return 0;
  }
  int size = atoi( argv[ 1 ] );
  int* A = new int[ size ];
  for (int i = 0; i < size; i++) 
    A[i] = rand();

  if ( argc < 4 )
    arrayDisplay("original array entries:", A, size);

  if ( 'b' == argv[2][0] || 'B' == argv[2][0] )
  { cerr << "now bubblesorting" << endl;
    bubbleSort( A, size );
  }
  else
  { cerr << "now quicksorting" << endl;
    quickSort( A, A + size - 1 );
  }

  if ( argc < 4 )
    arrayDisplay("sorted array entries:", A, size);

  cerr << comparisons << " comparisons and " 
       << swaps << " swaps used in sorting "
       << size << " element array" << endl;
}

