Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream> //for cout
- #include <time.h> //for timer
- #include <fstream>
- using namespace std;
- const int BIG = 1000000000;
- const int SIZE = 60000;
- template <class T>
- int searchList(T iaList[], const int NUM_ELEMS, T iValue);
- int iCompares;
- int main()
- {
- long startTime, endTime, elapsedTime;
- int iCount = 0, iIndex;
- string sWords[] = {"dog", "cat", "mouse", "Elephant"};
- string sWordsFind[] = {"PURPLE", "NURPLE", "PICTURE"};
- string sWordsInUnsorted[60000];
- ifstream fsUnsorted, fsSorted, fsSearch;
- int index = 0;
- // Open file
- fsUnsorted.open("words60k.txt");
- //
- while(fsUnsorted)
- {
- fsUnsorted >> sWordsInUnsorted[index];
- index++;
- }
- startTime = clock(); //get current clock value
- for (int i = 0; i < 3; i++)
- {
- iCompares = 0;
- iIndex = searchList(sWordsInUnsorted, SIZE, sWordsFind[i]);
- printf("iIndex = %i for %s and took %i compares\n",
- iIndex, sWordsFind[i].c_str(), iCompares);
- }
- endTime = clock();
- elapsedTime = endTime - startTime;
- cout << "\n\nThe operation took " << elapsedTime/static_cast<double>(CLOCKS_PER_SEC) << " seconds to do " << iCompares << " operations\n";
- return 0;
- }
- template <class T>
- int searchList (T ialist[], const int NUM_ELEMS, T iValue)
- {
- int index = 0;
- int iposition = -1;
- bool bfound = false;
- while (index < NUM_ELEMS && !bfound)
- {
- iCompares++;
- if (ialist[index] == iValue)
- {
- bfound = true;
- iposition = index;
- }
- index++;
- }
- return iposition;
- }
- // create array 60,000 strings
- // read the words into array
- // pass those into function to search
- // linear and binary search
Add Comment
Please, Sign In to add comment