Slide 8.10: Keysorting (cont.)
Slide 8.12: Bubblesort.h
Home

Keysort.cpp


The code below is the implementation of the main function of a keysort, which uses a bubble sort.

 ~wenchen/public_html/cgi-bin/351/week8/Keysort.cpp 
#include  <fstream>
#include  <cstring>
  using namespace  std;

#define   MaxStr     128
#define   MaxRecord  128

#include  "Bubblesort.h"
#include  "Book_1.h"
#include  "Overload.h"
#include  "WriteFile.h"


int  main( int argc, char* argv[ ] ) {
  fstream   infile;
  Book      b;
  int       length, number;
  char      key[MaxRecord][MaxStr];
  char      rrn[MaxRecord];
  char      buffer[MaxStr];

  infile.open( "books.txt", fstream::in );
  infile >> number >> length;
  for ( int i = 0;  i < number;  i++ ) {
    infile >> b;
    strcpy( key[i], b.GetValue( 2, buffer ) );
    rrn[i] = i;
  }
  Bubblesort( key, rrn, number );
  WriteFile ( infile, rrn, number, length );
  infile.close( );
}