20-CS-122-001 Computer Science II Spring 2012
Heapsort class

Virtual functions, classes, inheritance, lists, queues, stacks, applications

Prev     Next     Lectures     heapsort.cc

#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include "pqueue.h"
#include "bigint.h"
using namespace std;

BigInt *val(void *object) { return (BigInt*)object; }

int main(int argc, char **argv) {
   clock_t a,b,c;
   if (argc != 2 && argc != 3) {
      cerr << "Usage: " << argv[0] << " <number-of-elements> [-s]\n";
      exit(0);
   }
   bool flag = false;
   if (argc == 3) flag = true;

   int n = atoi(argv[1])-1;
   PQueue *pq = new PQueue(n,val,NULL);

   a = clock();
   for (int i=0 ; i < n ; i++) 
      pq->insert(new BigInt((int)(10.0*n*(rand()/(RAND_MAX+1.0)))));
   b = clock();

   if (flag) {
      for (int i=0 ; i < n ; i++) 
         cout << "\t" << (BigInt*)pq->remove();
      printf("\n");
   } else {
      for (int i=0 ; i < n ; i++) pq->remove();
   }
   c = clock();
   printf("Results: count:%d read:%f sort:%f\n",n+1,
          (b-a)/(CLOCKS_PER_SEC*1.0),(c-b)/(CLOCKS_PER_SEC*1.0));
}