Guest User

Untitled

a guest
Apr 21st, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.09 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include <limits.h>
  6.  
  7. #define MAX 20000
  8.  
  9. static int N ;
  10. static int a[MAX+1];
  11.  
  12. //================================= insertion sort ============================================
  13. void insertion (int a[], int N)
  14. {
  15. int i, j, v;
  16.  
  17. for (i = 2; i <= N; i++)
  18. {
  19. v = a[i]; j = i;
  20. while (a[j-1] > v)
  21. {a[j] = a[j-1]; j--;}
  22. a[j] = v;
  23. }
  24. }
  25.  
  26. //=============================== quicksort rekursiv =========================================
  27. void quicksort (int a[], int l, int r)
  28. {
  29. int v, i, j, t;
  30.  
  31. if (r > l)
  32. {
  33. v = a[r]; i = l-1; j = r;
  34. for (;;)
  35. {
  36. while (a[++i] < v) ;
  37. while (a[--j] > v) ;
  38. if (i >= j) break;
  39. t = a[i]; a[i] = a[j]; a[j] = t;
  40. }
  41. t = a[i]; a[i] = a[r]; a[r] = t;
  42. quicksort (a, l, i-1);
  43. quicksort (a, i+1, r);
  44. }
  45. }
  46.  
  47. //================================ shellsort ==============================================
  48. void shellsort (int a[], int N)
  49. {
  50. int i, j, h, v;
  51.  
  52. for (h = 1; h <= N/3; h = 3*h + 1) ;
  53. for ( ; h > 0; h /= 3)
  54. for (i = h + 1; i <= N; i++)
  55. {
  56. v = a[i]; j = i;
  57. while (j > h && a[j-h] > v)
  58. { a[j] = a[j-h]; j -= h;}
  59. a[j] = v;
  60. }
  61. }
  62.  
  63. // =============================== Heapsort =============================================
  64.  
  65. void upheap (int k)
  66. {
  67. int v;
  68.  
  69. v = a[k]; a[0] = INT_MAX;
  70. while (a[k/2] <= v)
  71. { a[k] = a[k/2]; k = k/2; }
  72. a[k] = v;
  73. }
  74.  
  75. void insert (int v)
  76. {
  77. a[++N] = v;
  78. upheap (N);
  79. }
  80.  
  81. void downheap (int k)
  82. {
  83. int j, v;
  84.  
  85. v = a[k];
  86. while (k <= N/2)
  87. {
  88. j = k+k;
  89. if (j < N && a[j] < a[j+1]) j++;
  90. if (v >= a[j]) break;
  91. a[k] = a[j]; k = j;
  92. }
  93. a[k] = v;
  94. }
  95.  
  96. int removes ()
  97. {
  98. int v = a[1];
  99.  
  100. a[1] = a[N--];
  101. downheap (1);
  102. return v;
  103. }
  104.  
  105. void heapsort (int a[], int laenge)
  106. {
  107. int k;
  108.  
  109. for (k = 1; k <= laenge; k++) insert (a[k]);
  110. for (k = laenge; k >= 1; k--) a[k] = removes();
  111. }
  112.  
  113.  
  114.  
  115. //===================================== Ende Sortierverfahren ==========================================
  116.  
  117. //array initaliesierung !
  118. void initAufsteigend(int array[],int size)
  119. {
  120. int index ;
  121. for (index = 1;index <= size;index++)
  122. {
  123. array[index] = index;
  124. }
  125. }
  126.  
  127. void initAbsteigend(int array[],int size)
  128. {
  129. int index ;
  130. int count = size;
  131.  
  132. for (index = 1; index <= size;index++)
  133. {
  134. array[index] = count;
  135. count--;
  136. }
  137. }
  138.  
  139. void initRandom(int array[],int size)
  140. {
  141. int index ;
  142.  
  143. // immer die gleichen zufallswerte
  144. srand(time(NULL));
  145.  
  146. for (index = 1; index <= size;index++)
  147. {
  148. array[index] = rand();
  149. }
  150. }
  151. void ausgabe (int array[],int size)
  152. {
  153. int index;
  154.  
  155. for (index = 1;index <= size;index++)
  156. {
  157. printf("%i ",array[index]);
  158. }
  159. printf("\n");
  160. }
  161.  
  162. void testSort()
  163. {
  164. int index ;
  165.  
  166. for (index = 1;index < MAX;index++)
  167. {
  168. if (!(a[index] <= a[index+1])){
  169. printf(" ============= ERROR SORT ============ ");
  170. }
  171. }
  172. }
  173. int main(void)
  174. {
  175.  
  176.  
  177. //absteigende eigenschaften
  178. int absteigend[6];
  179. int durschnittAbsteigend = 0.0;
  180.  
  181. //aufsteigende eigenschaften
  182. int aufsteigend[6];
  183. int durschnittAufsteigend = 0.0;
  184.  
  185. //random eigenschaften
  186. int random[6];
  187. int durschnittRandom = 0.0;
  188.  
  189. int index;
  190. int clocks;
  191.  
  192. // ==================================== insertion sort testing =================
  193.  
  194. for (index = 1 ;index <= 5 ;index++)
  195. {
  196.  
  197. initAbsteigend(a,MAX);
  198. clocks = clock();
  199. insertion(a,MAX);
  200. absteigend[index] = clock() - clocks ;
  201.  
  202.  
  203. initAufsteigend(a,MAX);
  204. clocks = clock();
  205. insertion(a,MAX);
  206. aufsteigend[index] = clock() - clocks ;
  207.  
  208.  
  209. initRandom(a,MAX);
  210. clocks = clock();
  211. insertion(a,MAX);
  212. random[index] = clock() - clocks ;
  213.  
  214. }
  215.  
  216. for (index = 1;index <= 5;index++)
  217. {
  218. durschnittAbsteigend += absteigend[index] ;
  219. durschnittAufsteigend += aufsteigend[index];
  220. durschnittRandom += random[index];
  221. }
  222.  
  223. printf("Insertion Sort\n");
  224. printf("Absteigende Sort. Durschnittszeit %d \n",durschnittAbsteigend/5);
  225. printf("Aufsteigende Sort. Durschnittszeit %d \n",durschnittAufsteigend/5);
  226. printf("Random Sort. Durschnittszeit %d \n",durschnittRandom/5);
  227.  
  228. // ============================= quicksort testing =========================
  229.  
  230. durschnittAbsteigend = 0.0;
  231. durschnittAufsteigend = 0.0;
  232. durschnittRandom = 0.0;
  233.  
  234. for (index = 1 ;index <= 5 ;index++)
  235. {
  236.  
  237. initAbsteigend(a,MAX);
  238. clocks = clock();
  239. //quicksort(a,1,MAX);
  240. absteigend[index] = clock() - clocks ;
  241. durschnittAufsteigend += clock() - clocks;
  242.  
  243. initAufsteigend(a,MAX);
  244. clocks = clock();
  245. //quicksort(a,1,MAX);
  246. aufsteigend[index] = clock() - clocks ;
  247.  
  248. initRandom(a,MAX);
  249. clocks = clock();
  250. quicksort(a,1,MAX);
  251. random[index] = clock() - clocks ;
  252. }
  253.  
  254.  
  255. for (index = 1;index <= 5;index++)
  256. {
  257. durschnittAbsteigend += absteigend[index] ;
  258. // durschnittAufsteigend += aufsteigend[index];
  259. durschnittRandom += random[index];
  260. }
  261.  
  262. printf("\nQuick Sort\n");
  263. printf("Absteigende Sort. Durschnittszeit %d \n",durschnittAbsteigend/5);
  264. printf("Aufsteigende Sort. Durschnittszeit %d \n",durschnittAufsteigend/5);
  265. printf("Random Sort. Durschnittszeit %d \n",durschnittRandom/5);
  266.  
  267. // ============================ ShellSort Testing ==========================================
  268.  
  269. durschnittAbsteigend = 0.0;
  270. durschnittAufsteigend = 0.0;
  271. durschnittRandom = 0.0;
  272.  
  273.  
  274. for (index = 1 ;index <= 5 ;index++)
  275. {
  276.  
  277. initAbsteigend(a,MAX);
  278. clocks = clock();
  279. shellsort(a,MAX);
  280. absteigend[index] = clock() - clocks;
  281.  
  282.  
  283. initAufsteigend(a,MAX);
  284. clocks = clock();
  285. shellsort(a,MAX);
  286. aufsteigend[index] = clock() - clocks;
  287.  
  288.  
  289. initRandom(a,MAX);
  290. clocks = clock();
  291. shellsort(a,MAX);
  292. random[index] = clock() - clocks;
  293.  
  294. }
  295.  
  296. for (index = 1;index <= 5;index++)
  297. {
  298. durschnittAbsteigend += absteigend[index] ;
  299. durschnittAufsteigend += aufsteigend[index];
  300. durschnittRandom += random[index];
  301. }
  302.  
  303. printf("\n Shell Sort\n");
  304. printf("Absteigende Sort. Durschnittszeit %d \n",durschnittAbsteigend/5);
  305. printf("Aufsteigende Sort. Durschnittszeit %d \n",durschnittAufsteigend/5);
  306. printf("Random Sort. Durschnittszeit %d \n",durschnittRandom/5);
  307.  
  308. // ====================================== HeapSort Testing =============================
  309.  
  310. durschnittAbsteigend = 0.0;
  311. durschnittAufsteigend = 0.0;
  312. durschnittRandom = 0.0;
  313.  
  314.  
  315. for (index = 1 ;index <= 5 ;index++)
  316. {
  317. N = 0;
  318.  
  319. initAbsteigend(a,MAX);
  320. clocks = clock();
  321. heapsort(a,MAX);
  322. absteigend[index] = clock() - clocks;
  323.  
  324. N = 0;
  325. initAufsteigend(a,MAX);
  326. clocks = clock();
  327. heapsort(a,MAX);
  328. aufsteigend[index] = clock() - clocks;
  329.  
  330. N = 0;
  331. initRandom(a,MAX);
  332. clocks = clock();
  333. heapsort(a,MAX);
  334. random[index] = clock() - clocks;
  335. testSort();
  336.  
  337. }
  338.  
  339. for (index = 1;index <= 5;index++)
  340. {
  341. durschnittAbsteigend += absteigend[index] ;
  342. durschnittAufsteigend += aufsteigend[index];
  343. durschnittRandom += random[index];
  344. }
  345.  
  346. printf("\nHeap Sort\n");
  347. printf("Absteigende Sort. Durschnittszeit %d \n",durschnittAbsteigend/5);
  348. printf("Aufsteigende Sort. Durschnittszeit %d \n",durschnittAufsteigend/5);
  349. printf("Random Sort. Durschnittszeit %d \n",durschnittRandom/5);
  350.  
  351. getchar();
  352. //ende main
  353. return 0;
  354. }
Add Comment
Please, Sign In to add comment