Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.65 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <conio.h>
  4. #include <time.h>
  5. #include <random>
  6. #include <string>
  7. #include <algorithm>
  8. using namespace std;
  9.  
  10. template <typename x>
  11. void Sort_insert(x*a, int n)
  12. {
  13. srand(time(0));
  14. unsigned int start = clock();
  15. int j;
  16. for (int i = 1; i < n; i++)
  17. {
  18. j = i;
  19. while (a[j - 1] > a[j] && j > 0)
  20. {
  21. swap(a[j - 1], a[j]);
  22. j--;
  23. }
  24. }
  25. unsigned int end = clock();
  26. cout << "Время выполнения Sort_Insert: " << double(end - start) / 1000 << endl;
  27. }
  28.  
  29. template <typename x>
  30. void ShellSort(x*a, int n)
  31. {
  32. srand(time(0));
  33. unsigned int start = clock();
  34. int i, j, step;
  35. x tmp;
  36. for (step = n / 2; step > 0; step /= 2)
  37. for (i = step; i < n; i++)
  38. {
  39. tmp = a[i];
  40. for (j = i; j >= step; j -= step)
  41. {
  42. if (tmp < a[j - step])
  43. a[j] = a[j - step];
  44. else
  45. break;
  46. }
  47. a[j] = tmp;
  48. }
  49. unsigned int end = clock();
  50. cout << "Время Выполнения Shell_Sort : " << double(end - start) / 1000 << endl;
  51. }
  52.  
  53. struct student
  54. {
  55. string name, surname;
  56. int marks[5];
  57.  
  58. bool operator < (student b)
  59. {
  60. if (surname != b.surname)
  61. return surname < b.surname;
  62. else return name < b.name;
  63. }
  64.  
  65. bool operator > (student b)
  66. {
  67. if (surname != b.surname)
  68. return surname > b.surname;
  69. else return name > b.name;
  70. }
  71.  
  72. bool operator == (student b)
  73. {
  74. return name == b.name && surname == b.surname;
  75. }
  76.  
  77.  
  78. void print()
  79. {
  80. cout << surname << " " << name << " ";
  81. for (int i = 0; i < 5; i++)
  82. cout << marks[i] << " ";
  83. cout << endl;
  84. }
  85. };
  86.  
  87. void random_students(student*c, student*c1, student*c2, int n)
  88. {
  89. string names[] = { "Andrey", "Sergey", "Nastya", "Alena", "Misha", "Sasha", "Vanya","Elena" };
  90. string surnames[] = { "Brown", "Green", "Yellow", "Black", "White", "Vaselenko", "Poroshenko", "Glinko", "Vasilisko", "Potter", "Portenko" };
  91. srand(time(0));
  92. for (int i = 0; i < n; i++)
  93. {
  94. c[i].name = names[rand() % 8];
  95. c1[i].name = c[i].name;
  96. c2[i].name = c[i].name;
  97. c[i].surname = surnames[rand() % 11];
  98. c1[i].surname = c[i].surname;
  99. c2[i].surname = c[i].surname;
  100. for (int j = 0; j < 5; j++)
  101. {
  102. c[i].marks[j] = 1 + rand() % 5;
  103. c1[i].marks[j] = c[i].marks[j];
  104. c2[i].marks[j] = c[i].marks[j];
  105. }
  106. }
  107.  
  108. }
  109.  
  110. void random_numbers(int*a, int* a1, int* a2, int n) //рандом чисел
  111. {
  112. srand(time(NULL)); // инициализируем рандом
  113.  
  114. for (int i = 0; i < n; i++)
  115. {
  116. a[i] = (rand() % RAND_MAX);
  117. a1[i] = a[i];
  118. a2[i] = a[i];
  119. }
  120. for (int i = 0; i < n; i++)
  121. cout << a[i] << " ";
  122. cout << endl;
  123.  
  124. }
  125.  
  126. void random_string(string*b, string* b1, string* b2, int n) //рандом стринга
  127. {
  128. srand(time(0));
  129. for (int i = 0; i < n; ++i)
  130. for (int j = 0; j < 1 + rand() % 5; j++)
  131. {
  132. b[i] += 97 + rand() % 25;
  133. b1[i] = b[i];
  134. b2[i] = b[i];
  135. }
  136. for (int i = 0; i < n; i++)
  137. cout << b[i] << " ";
  138. cout << endl;
  139. }
  140.  
  141. template <typename r>
  142. void printt(r*a, int n)
  143. {
  144. for (int i = 0; i < n; i++)
  145. cout << a[i] << ' ';
  146. }
  147.  
  148. int main()
  149. {
  150. setlocale(LC_CTYPE, "Russian");
  151. int n;
  152. cin >> n;
  153. int* a = new int[n];
  154. int* a1 = new int[n];
  155. int* a2 = new int[n];
  156. string* b = new string[n];
  157. string* b1 = new string[n];
  158. string* b2 = new string[n];
  159. student* c = new student[n];
  160. student* c1 = new student[n];
  161. student* c2 = new student[n];
  162.  
  163. random_numbers(a, a1, a2, n);
  164. random_string(b, b1, b2, n);
  165. random_students(c, c1, c2, n);
  166. for (int i = 0; i < n; i++)
  167. c[i].print();
  168. cout << endl;
  169.  
  170. cout << "Сортировка чисел: " << endl;
  171. Sort_insert(a, n);
  172. ShellSort(a1, n);
  173.  
  174. double time = clock();
  175. sort(a2, a2 + n);
  176. cout << "Время Выполнения STL_Sort : " << (clock() - time) / CLOCKS_PER_SEC << endl;
  177. cout << endl;
  178.  
  179. cout << "Сортировка строк:" << endl;
  180. Sort_insert(b, n);
  181. ShellSort(b1, n);
  182. time = clock();
  183. sort(b2, b2 + n);
  184. cout << "Время Выполнения STL_Sort : " << (clock() - time) / CLOCKS_PER_SEC << endl;
  185. cout << endl;
  186.  
  187. cout << "Сортировка студентов:" << endl;
  188. Sort_insert(c, n);
  189. ShellSort(c1, n);
  190. time = clock();
  191. sort(c2, c2 + n);
  192. cout << "Время Выполнения STL_Sort : " << (clock() - time) / CLOCKS_PER_SEC << endl;
  193. cout << endl;
  194.  
  195. /*printt(a, n);
  196. cout << endl;
  197. printt(a1, n);
  198. cout << endl;
  199. printt(a2, n);
  200. cout << endl;
  201. printt(b, n);
  202. cout << endl;
  203. printt(b1, n);
  204. cout << endl;
  205. printt(b2, n);
  206. cout << endl;
  207. for (int i = 0; i < n; i++)
  208. c[i].print();
  209. cout << endl;
  210. for (int i = 0; i < n; i++)
  211. c1[i].print();
  212. cout << endl;
  213. for (int i = 0; i < n; i++)
  214. c2[i].print();*/
  215.  
  216. return 0;
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement