binary search

closed account (G60iz8AR)
Does anyone know how to get the fast time for the words to pop up using binary search please ?

#include <fstream>
#include <iostream>
#include <string>
#include <stdlib.h>
#include <ctime>
#include <algorithm>

using namespace std;

const int DMAX = 173000;
const int JMAX = 50;
void loadArray( string a[], int &cnt, int cap, ifstream &infile, string filename );

int main( int argc, char**argv )
{
if (argc < 3 )
{
cout << " Hey you idiot! put the filenames after the a.exe!";
cout << "\nLIKE THIS: a.exe dictionary.txt jumbles.txt\n";
exit(0);
}
ifstream infile;
string dict[DMAX]; int dCnt=0;
string jumb[JMAX]; int jCnt=0;

clock_t start_time = clock();

loadArray( dict, dCnt, DMAX, infile, argv[1] );
sort( dict, dict+dCnt );
loadArray( jumb, jCnt, JMAX, infile, argv[2] );
sort( jumb, jumb+jCnt );



for( int i=0 ; i< jCnt ; i++ )
{
cout << jumb[i] << " ";
// make a jsorted copy of the jWord
// for each dWord in the dict array
// {
// make a dSorted copy of the dWord
// if jSorted == dSorted
// print dWord
// }
//
cout << endl;
}


cout << "\nElapsed time in seconds: " << float( clock () - start_time ) / CLOCKS_PER_SEC;

return 0;
} // END MAIN //###############################################################

void loadArray( string a[], int &cnt, int cap, ifstream &infile, string filename )
{
infile.open( filename.c_str(), ios::in );
while( cnt < cap && infile >> a[cnt] )
cnt++;
infile.close(); // Done reading. Close it
}
Topic archived. No new replies allowed.