Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.69 KB | None | 0 0
  1. // jd.cpp : Defines the entry point for the console application.
  2. //
  3. #include "pch.h"
  4. #include <iostream>
  5. #include <conio.h>
  6. #include <time.h>
  7. #include <cstdlib>
  8. #include <chrono>
  9.  
  10. int table_size;
  11.  
  12. void minmax1(int A[], int &j, int &k)
  13. {
  14. int i;
  15. int n = table_size;
  16. j = 0;
  17. for (i = 1; i < n; i++)
  18. {
  19. if (A[i] > A[j])
  20. {
  21. j = i;
  22. }
  23. }
  24. k = 0;
  25. for (i = 1; i < n; i++)
  26. {
  27. if (A[i] < A[k])
  28. {
  29. k = i;
  30. }
  31. }
  32. }
  33.  
  34. void minmax2(int A[], int &j, int &k)
  35. {
  36. int n = table_size;
  37. int i;
  38. j = 0; k = 0;
  39. for (i = 1; i < n; i++)
  40. if (A[i] > A[j])
  41. j = i;
  42. else if (A[i] < A[k])
  43. k = i;
  44. }
  45.  
  46. void minmax3(int A[], int &j, int &k)
  47. {
  48. int i, min, max;
  49. int n = table_size;
  50. if (A[0] > A[1])
  51. {
  52. k = 1;
  53. j = 0;
  54. }
  55. else
  56. {
  57. k = 0;
  58. j = 1;
  59. }
  60. i = 2;
  61. while (i < n - 1)
  62. {
  63. if (A[i] < A[i + 1])
  64. {
  65. min = i;
  66. max = i + 1;
  67. }
  68. else
  69. {
  70. min = i + 1;
  71. max = i;
  72. }
  73. if (A[min] < A[k])
  74. {
  75. k = min;
  76. }
  77. if (A[max] > A[j])
  78. {
  79. j = max;
  80. }
  81. i += 2;
  82. }
  83. if (i == n - 1)
  84. {
  85. if (A[n - 1] < A[k])
  86. {
  87. k = n - 1;
  88. }
  89. else
  90. {
  91. if (A[n - 1] < A[k])
  92. {
  93. j = n - 1;
  94. }
  95. }
  96. }
  97. }
  98.  
  99. int pot(int x, int y)
  100. {
  101. int potega = 1;
  102. while (y != 0)
  103. {
  104. if (y % 2 == 1)
  105. {
  106. potega *= x;
  107. }
  108. y /= 2;
  109. x *= x;
  110. }
  111. return potega;
  112. }
  113.  
  114. void wybieranie(int A[], int size)
  115. {
  116. int k, x;
  117. int n = table_size;
  118. for (int i = 0; i < n - 1; i++)
  119. {
  120. k = i;
  121. x = A[i];
  122. for (int j = i + 1; j < n; j++)
  123. {
  124. if (A[j] < x)
  125. {
  126. k = j;
  127. x = A[j];
  128. }
  129. A[k] = A[i];
  130. A[i] = x;
  131. }
  132. }
  133. }
  134.  
  135. void wstawianie(int A[], int size)
  136. {
  137. int j, x;
  138. int n = table_size;
  139. for (int i = 1; i < n; i++)
  140. {
  141. x = A[i];
  142. j = i - 1;
  143. while (j >= 0 && x < A[j])
  144. {
  145. A[j + 1] = A[j];
  146. j--;
  147. }
  148. A[j + 1] = x;
  149. }
  150. }
  151.  
  152. void zamiana(int &x, int &y)
  153. {
  154. int bufor;
  155. bufor = x;
  156. x = y;
  157. y = bufor;
  158. }
  159.  
  160. void babelkowe(int A[], int size)
  161. {
  162. int n = table_size;
  163. for (int i = 0; i < n - 1; i++)
  164. {
  165. for (int j = 0; j < n - i - 1; j++)
  166. {
  167. if (A[j] > A[j + 1])
  168. {
  169. zamiana(A[j], A[j + 1]);
  170. }
  171. }
  172. }
  173. }
  174.  
  175. void dod(int **A, int size)
  176. {
  177. int i, j, k;
  178. int n = table_size;
  179. for (i = 0; i < n; i++)
  180. {
  181. for (j = 0; j < n; j++)
  182. {
  183. for (k = j; k < n; k++)
  184. {
  185. A[i][j] += A[i][k];
  186. }
  187. }
  188. }
  189. }
  190.  
  191. using namespace std;
  192.  
  193. int main()
  194. {
  195. int j = 0;
  196. int k = 0;
  197. double duration;
  198. srand(time(NULL));
  199. table_size = 10;
  200. int *A = new int[table_size];
  201. for (int i = 0; i < table_size; i++)
  202. {
  203. A[i] = rand() % 10000;
  204. }
  205.  
  206. cout << "time\t\tminmax1\t\tminmax2\t\tminmax3" << endl;
  207.  
  208. // N = 10
  209. cout << "A[10]";
  210. auto start = chrono::high_resolution_clock::now();
  211. minmax1(A, j, k);
  212. auto stop = chrono::high_resolution_clock::now();
  213. cout << "\t\t" <<chrono::duration_cast<chrono::nanoseconds>(stop - start).count() << "ns";
  214.  
  215. start = chrono::high_resolution_clock::now();
  216. minmax2(A, j, k);
  217. stop = chrono::high_resolution_clock::now();
  218. cout << "\t\t" << chrono::duration_cast<chrono::nanoseconds>(stop - start).count() << "ns";
  219.  
  220. start = chrono::high_resolution_clock::now();
  221. minmax3(A, j, k);
  222. stop = chrono::high_resolution_clock::now();
  223. cout << "\t\t" << chrono::duration_cast<chrono::nanoseconds>(stop - start).count() << "ns";
  224. cout << endl;
  225. // N = 100
  226. table_size = 100;
  227. A = new int[table_size];
  228. for (int i = 0; i < table_size; i++)
  229. {
  230. A[i] = rand() % 10000;
  231. }
  232. cout << "A[100]";
  233. start = chrono::high_resolution_clock::now();
  234. minmax1(A, j, k);
  235. stop = chrono::high_resolution_clock::now();
  236. cout << "\t\t" << chrono::duration_cast<chrono::nanoseconds>(stop - start).count() << "ns";
  237.  
  238. start = chrono::high_resolution_clock::now();
  239. minmax2(A, j, k);
  240. stop = chrono::high_resolution_clock::now();
  241. cout << "\t\t" << chrono::duration_cast<chrono::nanoseconds>(stop - start).count() << "ns";
  242.  
  243. start = chrono::high_resolution_clock::now();
  244. minmax3(A, j, k);
  245. stop = chrono::high_resolution_clock::now();
  246. cout << "\t\t" << chrono::duration_cast<chrono::nanoseconds>(stop - start).count() << "ns";
  247. cout << endl;
  248. // N = 1000
  249. table_size = 1000;
  250. A = new int[table_size];
  251. for (int i = 0; i < table_size; i++)
  252. {
  253. A[i] = rand() % 10000;
  254. }
  255. cout << "A[1000]";
  256. start = chrono::high_resolution_clock::now();
  257. minmax1(A, j, k);
  258. stop = chrono::high_resolution_clock::now();
  259. cout << "\t\t" << chrono::duration_cast<chrono::nanoseconds>(stop - start).count() << "ns";
  260.  
  261. start = chrono::high_resolution_clock::now();
  262. minmax2(A, j, k);
  263. stop = chrono::high_resolution_clock::now();
  264. cout << "\t\t" << chrono::duration_cast<chrono::nanoseconds>(stop - start).count() << "ns";
  265.  
  266. start = chrono::high_resolution_clock::now();
  267. minmax3(A, j, k);
  268. stop = chrono::high_resolution_clock::now();
  269. cout << "\t\t" << chrono::duration_cast<chrono::nanoseconds>(stop - start).count() << "ns";
  270. cout << endl;
  271.  
  272. // N = 10000
  273. table_size = 10000;
  274. A = new int[table_size];
  275. for (int i = 0; i < table_size; i++)
  276. {
  277. A[i] = rand() % 10000;
  278. }
  279. cout << "A[10000]";
  280. start = chrono::high_resolution_clock::now();
  281. minmax1(A, j, k);
  282. stop = chrono::high_resolution_clock::now();
  283. cout << "\t" << chrono::duration_cast<chrono::nanoseconds>(stop - start).count() << "ns";
  284.  
  285. start = chrono::high_resolution_clock::now();
  286. minmax2(A, j, k);
  287. stop = chrono::high_resolution_clock::now();
  288. cout << "\t\t" << chrono::duration_cast<chrono::nanoseconds>(stop - start).count() << "ns";
  289.  
  290. start = chrono::high_resolution_clock::now();
  291. minmax3(A, j, k);
  292. stop = chrono::high_resolution_clock::now();
  293. cout << "\t\t" << chrono::duration_cast<chrono::nanoseconds>(stop - start).count() << "ns";
  294. cout << endl;
  295. _getch();
  296. delete[] A;
  297. return 0;
  298. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement