No Description

nHighest.cpp 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Measure execution times of two algorithms for computing
  2. // the 10 largest numbers in a list.
  3. //
  4. // Must compile using the -std=c++11 option.
  5. //
  6. // Usage:
  7. // ./<exec name> <size of list>
  8. #include <iostream>
  9. #include <vector>
  10. #include <cstdlib>
  11. #include <algorithm>
  12. #include <queue>
  13. using namespace std;
  14. //
  15. // Given a vector V and an integer n, copy the n largest
  16. // numbers to rest. This algorithm uses sorting.
  17. //
  18. void nHighest(vector <float> &V, int n, vector <float> &res) {
  19. sort(V.begin(), V.end());
  20. res.resize(n);
  21. copy(V.begin()+V.size()-n, V.end(), res.begin());
  22. }
  23. //
  24. // Given a vector V and an integer n, copy the n largest
  25. // numbers to rest. This algorithm uses a priority queue.
  26. //
  27. void nHighestWithPQ(const vector <float> &V, int n, vector <float> &res) {
  28. res.resize(0);
  29. priority_queue<float, std::vector<float> , std::greater<float>> q;
  30. // put the first n+1 elements into the queue
  31. for (int i = 0; i <= n; i++) q.push(V[i]);
  32. // for the rest, if they are higher than the smallest in the queue,
  33. // then enqueue
  34. for (int i = n+1; i < V.size(); i++) {
  35. if (V[i] > q.top()) {
  36. q.pop(); // pop the 11th largest element
  37. q.push(V[i]); // push one element
  38. }
  39. }
  40. q.pop();
  41. while (!q.empty()) {
  42. res.push_back(q.top());
  43. q.pop();
  44. }
  45. }
  46. int main(int argc, char *argv[]) {
  47. // get the command line argument
  48. if (argc < 2) {
  49. cout << "Usage: " << argv[0] << " <size> \n";
  50. exit(1);
  51. }
  52. int size = atoi(argv[1]);
  53. vector<float> v,w;
  54. // fill the v vector with random integers
  55. for (int i = 0; i < size; i++)
  56. v.push_back(rand() / static_cast<float>(RAND_MAX));
  57. // copy v to w
  58. w = v;
  59. // initialize the random seed
  60. srand(time(NULL));
  61. vector<float> res1, res2;
  62. double elapsed_secs;
  63. // measure the time for the sorting version
  64. clock_t begin = clock();
  65. nHighest(v, 10, res1);
  66. clock_t end = clock();
  67. elapsed_secs = static_cast<double>(end - begin) / CLOCKS_PER_SEC;
  68. cout << "\nAlgorithm A: " << elapsed_secs << " seconds\n";
  69. // measure the time for the priority queue version
  70. begin = clock();
  71. nHighestWithPQ(w, 10, res2);
  72. end = clock();
  73. elapsed_secs = static_cast<double>(end - begin) / CLOCKS_PER_SEC;
  74. cout << "Algorithm B: " << elapsed_secs << " seconds\n";
  75. #ifdef DEBUG
  76. cout << "Results for algorithm A:\n";
  77. for (auto e: res1) cout << e <<" ";
  78. cout << '\n';
  79. cout << "Results for algorithm B:\n";
  80. for (auto e: res1) cout << e <<" ";
  81. cout << '\n';
  82. #endif
  83. if (res1 == res2)
  84. cout << "Congratulations. Both algorithms had the ame results!\n\n";
  85. else
  86. cout << "Bad new. The algorithms obtained different results :-(\n";
  87. return 0;
  88. }
  89. // 125000
  90. // 250000
  91. // 500000
  92. // 1000000