Advertisement
Guest User

Untitled

a guest
May 20th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. //
  2. // hw3.h
  3. // project3
  4. //
  5. // Created by Joel Hayashi on 2019/05/02.
  6. // Copyright © 2019 Joel Hayashi. All rights reserved.
  7. //
  8. // Joel Hayashi
  9. // May 6, 2019
  10. //
  11. // Program implements the file average_complexity ( int ) which does a linear search for a random number in a vector of ints that are randomly aligned and returns a double which indicates the average running time of the linear search based on its number of comparisons
  12. //
  13. //
  14. // This program has a running time of T = O ( N^2 ), theoretically. Experimentally, it has a running time of T = O ( N )
  15.  
  16. #ifndef hw3_h
  17. #define hw3_h
  18.  
  19. #include <iostream>
  20. #include <vector>
  21. #include <cstdlib>
  22. #include <algorithm>
  23.  
  24.  
  25. /**template< class RandomIt > //This is the implementation of std::random_shuffle from the internet...only one for loop so theoretically, O ( N )
  26. void random_shuffle( RandomIt first, RandomIt last )
  27. {
  28. typename std::iterator_traits<RandomIt>::difference_type i, n;
  29. n = last - first;
  30. for (i = n-1; i > 0; --i) {
  31. using std::swap;
  32. swap(first[i], first[std::rand() % (i+1)]);
  33. // rand() % (i+1) isn't actually correct, because the generated number
  34. // is not uniformly distributed for most values of i. A correct implementation
  35. // will need to essentially reimplement C++11 std::uniform_int_distribution,
  36. // which is beyond the scope of this example.
  37. }
  38. }
  39. */
  40.  
  41. double average_complexity( int x ){
  42. std::vector<int> randomVector;
  43.  
  44. double prevRunningTime = 0.0; //For comparison purposes
  45. double avgRunningTime = 0.0;
  46.  
  47. for (int i = 1; i <= x; i++){randomVector.push_back(i);} //Line up all numbers from 1 to x in vector
  48. std::random_shuffle(randomVector.begin(), randomVector.end()); //Randomly shuffle the elements in the vector
  49.  
  50. //Initiate iterations to search for random numbers within the vector
  51. long int operationNum = 0; //Keeps track of number of operations done
  52. int randomNums; //Number to be searched
  53.  
  54. for (double j = 1; j <= x; j++) { //First stopping condition is the number of loops which is the int passed to average_complexity
  55. randomNums = 1 + rand() % x;
  56. for (int k = 0; k < randomVector.size(); k++) { //Looking through each entry
  57. if (randomVector[k] == randomNums){
  58. operationNum += (k + 1); //Must add k + 1 since k starts from 0
  59. break;
  60. }
  61. }
  62.  
  63. //This nested for loop would indicate an O ( N^2 ), which takes precedence over the O ( N ) in random_shuffle
  64.  
  65. if (j > 1) {prevRunningTime = avgRunningTime;} //Update prevRunningTime to last avgRunningTime
  66. avgRunningTime = operationNum / j; //Update avgRunningTime per run
  67.  
  68. if (avgRunningTime >= (1 - 0.005) * prevRunningTime && avgRunningTime <= (1 + 0.005) * prevRunningTime && j >= 500) {break;} //Final stopping condition is when the difference in running time is marginal, within +/- 0.5% of the previously recording avg running time
  69. }
  70. return avgRunningTime;
  71. }
  72.  
  73.  
  74.  
  75. #endif /* hw3_h */
  76.  
  77. // Test runs
  78. // int const MAX_N = 8192000; // 405 sec , 485 sec , 418 sec
  79. // int const MAX_N = 4096000; // 224 sec , 220 sec , 215 sec , 216 sec
  80. // int const MAX_N = 2048000; // 104 sec , 97 sec , 109 sec , 102 sec
  81. // int const MAX_N = 1024000; // 52 sec , 52 sec , 51 sec , 47 sec , 52 sec
  82. // int const MAX_N = 512000; // 26 sec , 26 sec , 25 sec , 24 sec
  83. // int const MAX_N = 256000; // 14 sec , 13 sec , 12 sec , 13 sec
  84. // int const MAX_N = 128000; // 6 sec , 6 sec , 7 sec , 6 sec
  85. // int const MAX_N = 64000; // 2 sec , 3 sec , 3 sec , 3 sec
  86. // int const MAX_N = 32000; // 2 sec , 1 sec , 1 sec , 1 sec
  87. // int const MAX_N = 16000; // 0 sec , 0 sec , 0 sec
  88. // int const MAX_N = 8000; // 0 sec , 0 sec , 0 sec
  89. // int const MAX_N = 4000; // 0 sec , 0 sec , 0 sec
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement