Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.27 KB | None | 0 0
  1. // ConsoleApplication1.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include <iostream>
  5. #include <ctime>
  6. using namespace std;
  7. struct DATE
  8. {
  9. double time;
  10. unsigned int countComprasion=0;
  11. unsigned int countAssigmant=0;
  12. };
  13. void InitArrayInc(int *temp, int N)
  14. {
  15. for (int i = 0; i < N; i++)
  16. {
  17. temp[i] = i;
  18. }
  19. }
  20. void InitArrayDec(int *temp, int N)
  21. {
  22. for (int i = N,j=0; j<N; i--,j++)
  23. {
  24. temp[j] = i;
  25. }
  26. }
  27. void InitArrayRand(int *temp, int N)
  28. {
  29. for (int i = 0; i < N; i++)
  30. {
  31. temp[i] = rand()%1000+1;
  32. }
  33. }
  34. void BubbleSort(int *tempA, int N, DATE* tempD,int k,double &ref)
  35. {
  36. int temp;
  37. for (int i = 0; i < N - 1; i++)
  38. {
  39. for (int j = 0; j < (N - 1); j++)
  40. {
  41. if (tempA[j] < tempA[j + 1])
  42. {
  43. temp = tempA[j];
  44. tempA[j] = tempA[j + 1];
  45. tempA[j + 1] = temp;
  46. tempD[k].countComprasion += 3;
  47. }
  48. tempD[k].countAssigmant++;
  49. }
  50. }
  51. ref= (clock() - ref) / 1000.0;
  52. tempD[k].time = ref;
  53. }
  54. void QuickSort(int *tArray, int first, int last,int L, DATE* tempD, int k,double& ref)
  55. {
  56. int pivot, temp;
  57. int i = first, j = last;
  58. if (L==1)
  59. {
  60. pivot = tArray[(i + j) / 2];
  61. }
  62. if (L == 2)
  63. {
  64. pivot = tArray[j];
  65. }
  66. while(i<j)
  67. {
  68. while (tArray[i] < pivot)
  69. {
  70. i++;
  71. tempD[k].countComprasion++;
  72. }
  73. tempD[k].countComprasion++;
  74. while (tArray[j] > pivot)
  75. {
  76. j--;
  77. tempD[k].countComprasion++;
  78. }
  79. tempD[k].countComprasion++;
  80. if (i <= j) //перестановка элементов
  81. {
  82. temp = tArray[i];
  83. tArray[i] = tArray[j];
  84. tArray[j] = temp;
  85. i++;
  86. j--;
  87. tempD[k].countComprasion++;
  88. tempD[k].countAssigmant += 3;
  89. }
  90. }
  91. if (first<j)
  92. {
  93. tempD[k].countComprasion++;
  94. QuickSort(tArray, first, j,L,tempD,k,ref);
  95. }
  96. if (last > i)
  97. {
  98. tempD[k].countComprasion++;
  99. QuickSort(tArray, i, last, L, tempD, k,ref);
  100. }
  101. tempD[k].time = clock() / 1000.0 - ref;
  102. }
  103. void Print(DATE* tempD,int N)
  104. {
  105. cout << "Сравнения" << endl;
  106. for (int i = 0; i < N; i++)
  107. {
  108. cout << i+1 << ") ";
  109. cout << tempD[i].countAssigmant<<" ";
  110. }
  111. cout << endl;
  112. cout << "Присваивания" << endl;
  113. for (int i = 0; i < N; i++)
  114. {
  115. cout << i + 1 << ") ";
  116. cout << tempD[i].countComprasion << " ";
  117. }
  118. cout << endl;
  119. cout << "Время" << endl;
  120. for (int i = 0; i < N; i++)
  121. {
  122. cout << i + 1 << ") ";
  123. cout << tempD[i].time << " ";
  124. }
  125. }
  126. void Clean(DATE* tempD)
  127. {
  128. for (int i = 0; i < 6; i++)
  129. {
  130. tempD[i].countAssigmant = 0;
  131. tempD[i].countComprasion = 0;
  132. tempD[i].time = 0;
  133. }
  134. }
  135. void Print(int* temp,int N)
  136. {
  137. for (int i = 0; i < N; i++)
  138. {
  139. cout << temp[i] << " ";
  140. }
  141. }
  142. int main()
  143. {
  144. setlocale(LC_ALL, "RUS");
  145. const int N = 100, NR1 = 10, NR2 = 1000, NR3 = 10000, NR4 = 100000;
  146. int arrayIncrease[N], arrayDecrease[N],arrayRandomOne[NR1], arrayRandomTwo[NR2], arrayRandomThree[NR3], arrayRandomFour[NR4];
  147. double arrayTime[20];
  148. DATE obj[30];
  149. double difTime = 0;
  150. double& refDifT=difTime;
  151.  
  152. InitArrayInc(arrayIncrease, N);
  153. InitArrayDec(arrayDecrease, N);
  154. InitArrayRand(arrayRandomOne, NR1);
  155. InitArrayRand(arrayRandomTwo, NR2);
  156. InitArrayRand(arrayRandomThree, NR3);
  157. InitArrayRand(arrayRandomFour, NR4);
  158.  
  159. srand(time(0));
  160. refDifT = clock()/1000.0;
  161. BubbleSort(arrayIncrease, N,obj,0, refDifT);
  162. BubbleSort(arrayDecrease, N,obj, 1, refDifT);
  163. BubbleSort(arrayRandomOne,NR1, obj,2, refDifT);
  164. BubbleSort(arrayRandomTwo,NR2,obj,3, refDifT);
  165. BubbleSort(arrayRandomThree,NR3,obj,4, refDifT);
  166. BubbleSort(arrayRandomFour,NR4,obj,5, refDifT);
  167. cout << "Сортировка пузырьком" << endl;
  168. Print(obj, 6);
  169. cout << endl;
  170. Clean(obj);
  171. cout << endl;
  172. QuickSort(arrayIncrease, 0,N-1,1, obj, 0, refDifT);
  173. QuickSort(arrayDecrease,0, N-1,1, obj, 1, refDifT);
  174. QuickSort(arrayRandomOne, 0,NR1-1,1, obj, 2, refDifT);
  175. QuickSort(arrayRandomTwo, 0,NR2-1,1, obj, 3, refDifT);
  176. QuickSort(arrayRandomThree, 0,NR3-1,1, obj, 4, refDifT);
  177. QuickSort(arrayRandomFour, 0,NR4-1,1, obj, 5, refDifT);
  178. cout << "Быстрая сортировка(1)" << endl;
  179. Print(obj, 6);
  180. cout << endl;
  181. Clean(obj);
  182. QuickSort(arrayIncrease, 0, N - 1, 1, obj, 0, refDifT);
  183. QuickSort(arrayDecrease, 0, N - 1, 1, obj, 1, refDifT);
  184. QuickSort(arrayRandomOne, 0, NR1 - 1, 2, obj, 2, refDifT);
  185. QuickSort(arrayRandomTwo, 0, NR2 - 1, 1, obj, 3, refDifT);
  186. QuickSort(arrayRandomThree, 0, NR3 - 1, 1, obj, 4, refDifT);
  187. QuickSort(arrayRandomFour, 0, NR4 - 1, 1, obj, 5, refDifT);
  188. cout << endl;
  189. cout << "Быстрая сортировка(2)" << endl;
  190. Print(obj, 6);
  191. cout << endl;
  192. Clean(obj);
  193. cout << "Проверка пузырька:" << endl;
  194. InitArrayRand(arrayRandomOne, NR1);
  195. cout << "До сортировки" << endl;
  196. Print(arrayRandomOne, NR1);
  197. cout << endl;
  198. BubbleSort(arrayRandomOne, NR1, obj, 2, refDifT);
  199. cout << "После сортировки" << endl;
  200. Print(arrayRandomOne, NR1);
  201. cout << endl<<endl;
  202. cout << "Проверка быстрой сортировки:" << endl;
  203. InitArrayRand(arrayRandomOne, NR1);
  204. cout << "До сортировки" << endl;
  205. Print(arrayRandomOne, NR1);
  206. cout << endl;
  207. QuickSort(arrayRandomOne, 0, NR1 - 1, 1, obj, 2, refDifT);
  208. cout << "После сортировки" << endl;
  209. Print(arrayRandomOne, NR1);
  210. return 0;
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement