Advertisement
Guest User

C++ sorting clock ting

a guest
Feb 21st, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.26 KB | None | 0 0
  1.  
  2. //================================================================================
  3. // Name        : practical4.cpp
  4. // Author      :
  5. // Version     : 1.0
  6. // Description : practical 4 test program to utilise sort algorithms
  7. //================================================================================
  8.  
  9. #include <string> // used to access string type
  10. #include <cctype>
  11. #include <ctime> // used to access clock_t and clock()
  12.  
  13. #include "Array.h"
  14. #include "Sorter.h"
  15.  
  16. using namespace std;
  17.  
  18. // Test relative sort performances when sorting same
  19. // random and sorted number sequence.
  20. void testPerformance(int size) {
  21.     clock_t start, start1, finish, finish1;
  22.  
  23.     // Create a sorter of specified size for one sort
  24.     // then fill sorter with random numbers
  25.     // Create sorters for other sorts and initialise with the
  26.     // random numbers in the first sorter.
  27.     // Consider what Sorter methods/constructors should be used
  28.     // time sort of random data
  29.  
  30.  
  31.         // create a Sorter for each sort algorithm ensuring each
  32.         // contain the same random numbers. Use of Sorter copy
  33.         // constructor is one way to ensure this!!
  34.     Sorter<int> qsorter(size);
  35.     qsorter.fillRandom(size);
  36.     Sorter<int> isorter(qsorter);
  37.     Sorter<int> msorter(qsorter);
  38.     Sorter<int> ssorter(qsorter);
  39.  
  40.     //Quick Sort
  41.     start = clock();
  42.     qsorter.quickSort();
  43.     finish = clock();
  44.     start1 = clock();
  45.     qsorter.quickSort();
  46.     finish1 = clock();
  47.     cout << "Quick Sort " << size << " Random/Sorted = " << (finish - start) << "/" << (finish1 - start1) << endl;
  48.  
  49.  
  50.     //Selection Sort
  51.     start = clock();
  52.     ssorter.selectionSort();
  53.     finish = clock();
  54.     start1 = clock();
  55.     ssorter.selectionSort();
  56.     finish1 = clock();
  57.     cout << "Selection Sort " << size << " Random/Sorted = " << (finish - start) << "/" << (finish1 - start1) << endl;
  58.  
  59.     //Insertion Sort
  60.     start = clock();
  61.     isorter.insertionSort();
  62.     finish = clock();
  63.     start1 = clock();
  64.     isorter.insertionSort();
  65.     finish1 = clock();
  66.     cout << "Insertion Sort " << size << " Random/Sorted = " << (finish - start) << "/" << (finish1 - start1) << endl;
  67.  
  68.     //Merge Sort   
  69.     start = clock();
  70.     msorter.mergeSort();
  71.     finish = clock();
  72.     start1 = clock();
  73.     msorter.mergeSort();
  74.     finish1 = clock();
  75.     cout << "Merge Sort " << size << " Random/Sorted = " << (finish - start) << "/" << (finish1 - start1) << endl;
  76. }
  77.  
  78. // Determine the most frequent word in the supplied text file 'words.txt'
  79. // print the word and its frequency
  80. void wordFrequency() {
  81.     Sorter<string> words;
  82.  
  83.     // complete method by loading content of test.txt into 'words' sorter
  84.     // defined above (use the sorter loadFile method). Sort the content by
  85.     // (calling a sorter sort methd (any will do), then extract the sorted
  86.     // array from the sorter (use the sorter getElements() method). Finally
  87.     // loop through the sorted array and determine the most frequently occuring word
  88.     // print the word and the number of occurences
  89. }
  90.  
  91.  
  92.  
  93. // Main method.
  94. int main() {
  95.     //initialise the random number generator
  96.     srand((unsigned)time(NULL));
  97.     testPerformance(500000);
  98.     // q1 testPerformance
  99.     // create a loop to call with values e.g. 10000,15000,2000
  100.  
  101.     // call the word frequency method to solve q2
  102.  
  103.     // ---------------------------------------------------
  104.     cout << endl << "Press enter to quit";
  105.     cin.get();
  106.     return 0;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement