Guest User

Untitled

a guest
Jan 24th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.89 KB | None | 0 0
  1. #include <iostream>     //for cout
  2. #include <time.h>       //for timer
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. const int BIG = 1000000000;
  8. const int SIZE = 60000;
  9. template <class T>
  10. int searchList(T iaList[], const int NUM_ELEMS, T iValue);
  11. int iCompares;
  12. int main()
  13. {
  14.     long startTime, endTime, elapsedTime;
  15.     int iCount = 0, iIndex;
  16.    
  17.     string sWords[] = {"dog", "cat", "mouse", "Elephant"};
  18.     string sWordsFind[] = {"PURPLE", "NURPLE", "PICTURE"};
  19.     string sWordsInUnsorted[60000];
  20.     ifstream fsUnsorted, fsSorted, fsSearch;
  21.     int index = 0;
  22.    
  23.     // Open file
  24.     fsUnsorted.open("words60k.txt");
  25.     //
  26.     while(fsUnsorted)
  27.     {
  28.         fsUnsorted >> sWordsInUnsorted[index];
  29.         index++;
  30.     }
  31.  
  32.     startTime = clock();        //get current clock value
  33.     for (int i = 0; i < 3; i++)
  34.     {  
  35.         iCompares = 0;
  36.         iIndex = searchList(sWordsInUnsorted, SIZE, sWordsFind[i]);
  37.         printf("iIndex = %i for %s and took %i compares\n",
  38.                iIndex, sWordsFind[i].c_str(), iCompares);  
  39.     }
  40.        
  41.     endTime = clock();
  42.     elapsedTime = endTime - startTime;
  43.     cout << "\n\nThe operation took " << elapsedTime/static_cast<double>(CLOCKS_PER_SEC) << " seconds to do " << iCompares << " operations\n";
  44.    
  45.     return 0;
  46. }
  47.  
  48.  
  49. template <class T>
  50.     int searchList (T ialist[], const int NUM_ELEMS, T iValue)
  51.    
  52. {
  53.         int index = 0;
  54.         int iposition = -1;
  55.         bool bfound = false;
  56.        
  57.         while (index < NUM_ELEMS && !bfound)
  58.         {
  59.             iCompares++;
  60.            
  61.             if (ialist[index] == iValue)
  62.             {
  63.                 bfound = true;
  64.             iposition = index;
  65.                
  66.             }
  67.             index++;
  68.         }
  69.         return iposition;
  70.     }
  71.  
  72. // create array 60,000 strings
  73.  
  74. // read the words into array
  75. // pass those into function to search
  76. // linear and binary search
Add Comment
Please, Sign In to add comment