Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. #include <list>
  2. #include <vector>
  3. #include <iostream>
  4. #include <iomanip>
  5. #include <string>
  6. #include <ctime>
  7. #include <cstdlib>
  8. using namespace std;
  9.  
  10. void insert(list<short>& l, const short& value);
  11. void insert(vector<short>& v, const short& value);
  12. void insert(short arr[], int& logicalSize, const int& physicalSize, const short& value);
  13.  
  14. int main() {
  15. clock_t start, end;
  16. srand(time(nullptr));
  17.  
  18. const int SIZE = 50000;
  19. const short RANGE = 10000;
  20. list<short> l;
  21. vector<short> v;
  22. short* arr = new short[SIZE];
  23. int logicalSize = 0;
  24.  
  25. // array
  26. start = clock();
  27. cout << "Array storage time test...";
  28. for (int i = 0; i < SIZE; i++) {
  29. try {
  30. insert(arr, logicalSize, SIZE, (short)(rand() % (2 * RANGE + 1) - RANGE));
  31. } catch (string s) {
  32. cout << s << endl;
  33. system("pause");
  34. exit(-1);
  35. }
  36. }
  37. end = clock();
  38. cout << "Time: " << difftime(end, start) << endl << endl;
  39.  
  40. // list
  41. cout << "List storage time test...";
  42. start = clock();
  43. for (int i = 0; i < SIZE; i++) {
  44. insert(l, (short)(rand() % (2 * RANGE + 1) - RANGE));
  45. }
  46. end = clock();
  47. cout << "Time: " << difftime(end, start) << endl << endl;
  48.  
  49. // vector
  50. cout << "Vector storage time test...";
  51. start = clock();
  52. for (int i = 0; i < SIZE; i++) {
  53. insert(v, (short)(rand() % (2 * RANGE + 1) - RANGE));
  54. }
  55. end = clock();
  56. cout << "Time: " << difftime(end, start) << endl << endl;
  57.  
  58.  
  59.  
  60. delete[] arr;
  61. system("pause");
  62. return 0;
  63. }
  64.  
  65. void insert(list<short>& l, const short& value) {
  66. for (auto it = l.begin(); it != l.end(); it++) {
  67. if (value < *it) {
  68. l.insert(it, value);
  69. return;
  70. }
  71. }
  72. l.push_back(value);
  73. }
  74.  
  75. void insert(vector<short>& v, const short& value) {
  76. for (auto it = v.begin(); it != v.end(); it++) {
  77. if (value < *it) {
  78. v.insert(it, value);
  79. return;
  80. }
  81. }
  82. v.push_back(value);
  83. }
  84.  
  85. void insert(short arr[], int& logicalSize, const int& physicalSize, const short& value) {
  86. if (logicalSize == physicalSize) throw string("No spaces in array.");
  87. for (int i = 0; i < logicalSize; i++) {
  88. if (value < arr[i]) {
  89. for (int j = logicalSize - 1; j >= i; j--) {
  90. arr[j + 1] = arr[j];
  91. }
  92. arr[i] = value;
  93. logicalSize++;
  94. return;
  95. }
  96. }
  97. arr[logicalSize] = value;
  98. logicalSize++;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement