Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. // pract3.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
  2. //
  3.  
  4. #include "pch.h"
  5. #include <iostream>
  6. #include <ctime>
  7. #include <time.h>
  8.  
  9. using namespace std;
  10.  
  11. unsigned int inclusionSort(int* const &num, int size) //сортировка включением
  12. {
  13. unsigned int start_time = clock();
  14. for (int i = 1; i < size; i++)
  15. {
  16. int value = num[i];
  17. int index = i;
  18. while ((index > 0) && (num[index - 1] > value))
  19. {
  20. num[index] = num[index - 1];
  21. index--;
  22. }
  23. num[index] = value;
  24. }
  25. unsigned int end_time = clock();
  26. return end_time - start_time;
  27. }
  28.  
  29. unsigned int selectionSort(int* const &num, int size) //сортировка выбором
  30. {
  31. unsigned int start_time = clock();
  32. int min, temp;
  33. for (int i = 0; i < size - 1; i++)
  34. {
  35. min = i;
  36. for (int j = i + 1; j < size; j++)
  37. {
  38. if (num[j] < num[min])
  39. min = j;
  40. }
  41. temp = num[i];
  42. num[i] = num[min];
  43. num[min] = temp;
  44. }
  45. unsigned int end_time = clock();
  46. return end_time - start_time;
  47. }
  48.  
  49. unsigned int bubbleSort(int* const &num, int size) //сортировка обменом
  50. {
  51. unsigned int start_time = clock();
  52. for (int i = 0; i < size - 1; i++)
  53. {
  54. for (int j = (size - 1); j > i; j--)
  55. {
  56. if (num[j - 1] > num[j])
  57. {
  58. int temp = num[j - 1];
  59. num[j - 1] = num[j];
  60. num[j] = temp;
  61. }
  62. }
  63. }
  64. unsigned int end_time = clock();
  65. return end_time - start_time;
  66. }
  67.  
  68. int shekerSort(int* const &num, int count)
  69. {
  70. int permutations = 0;
  71. int left = 0, right = count - 1;
  72. int flag = 1;
  73. while ((left < right) && flag > 0)
  74. {
  75. flag = 0;
  76. for (int i = left; i < right; i++)
  77. {
  78. if (num[i] > num[i + 1])
  79. {
  80. int t = num[i];
  81. num[i] = num[i + 1];
  82. num[i + 1] = t;
  83. flag = 1;
  84. permutations++;
  85. }
  86. }
  87. right--;
  88. for (int i = right; i > left; i--)
  89. {
  90. if (num[i - 1] > num[i])
  91. {
  92. int t = num[i];
  93. num[i] = num[i - 1];
  94. num[i - 1] = t;
  95. flag = 1;
  96. permutations++;
  97. }
  98. }
  99. left++;
  100. }
  101. return permutations;
  102. }
  103.  
  104. int shellSort(int* const &num, int size)
  105. {
  106. int permutations = 0;
  107. int increment = 3;
  108. while (increment > 0)
  109. {
  110. for (int i = 0; i < size; i++)
  111. {
  112. int j = i;
  113. int temp = num[i];
  114.  
  115. while ((j >= increment) && (num[j - increment] > temp))
  116. {
  117. num[j] = num[j - increment];
  118. j = j - increment;
  119. }
  120. num[j] = temp;
  121. permutations++;
  122. }
  123. if (increment > 1)
  124. increment = increment / 2;
  125. else if (increment == 1)
  126. break;
  127. }
  128. return permutations;
  129. }
  130.  
  131. int main()
  132. {
  133. setlocale(LC_ALL, "RUS");
  134. int size = 35;
  135. int* num = new int[size];
  136. srand((unsigned)time(NULL));
  137. cout << "Дан массив: " << endl;
  138. for (int i = 0; i < size; i++)
  139. {
  140. num[i] = -100 + rand() % 200;
  141. cout << num[i] << " ";
  142. }
  143. cout << endl;
  144. cout << endl;
  145. cout << "Сортировка прямым включением\tвремя\t" << inclusionSort(num, size) << endl;
  146. cout << "Сортировка прямым выбором\tвремя\t" << selectionSort(num, size) << endl;
  147. cout << "Сортировка прямым обменом\tвремя\t" << bubbleSort(num, size) << endl;
  148. cout << endl;
  149. cout << "Сортировка методом Шейкера\tколичество перестановок\t" << shekerSort(num, size) << endl;
  150. cout << "Сортировка методом Шелла\tколичество перестановок\t" << shellSort(num, size) << endl;
  151. cout << endl;
  152. system("pause");
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement