Guest User

Untitled

a guest
Aug 8th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. /* bubblesort.c
  2. *
  3. * Bubblesort sorting algorithm implemented recursively.
  4. *
  5. * Copyright (C) David Thomas, 2010-2017.
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11.  
  12. /* ----------------------------------------------------------------------- */
  13.  
  14. /* recursive bubblesort interior */
  15. static int bubblesort_swap(int *a, int nelems, int depth, int highest)
  16. {
  17. /* we test pairs of numbers. so the final case is when nelems is one, not
  18. * zero */
  19. if (nelems <= 1)
  20. return highest;
  21.  
  22. if (a[0] > a[1])
  23. {
  24. /* swap */
  25. int t = a[0];
  26. a[0] = a[1];
  27. a[1] = t;
  28.  
  29. highest = depth;
  30. }
  31.  
  32. return bubblesort_swap(a + 1, nelems - 1, depth + 1, highest);
  33. }
  34.  
  35. /* recursive bubblesort exterior */
  36. static void bubblesort__recursive(int *a, int nelems)
  37. {
  38. int highest;
  39.  
  40. highest = bubblesort_swap(a,
  41. nelems,
  42. 1 /* initial depth */,
  43. 0 /* initial 'highest' */);
  44. if (highest > 0)
  45. bubblesort__recursive(a, highest);
  46. }
  47.  
  48. /* iterative bubblesort */
  49. static void bubblesort__loop(int *a, int nelems)
  50. {
  51. int max = nelems - 1;
  52.  
  53. do
  54. {
  55. int i;
  56. int highest;
  57.  
  58. /* swap overlapping pairs of values */
  59. highest = 0;
  60.  
  61. for (i = 0; i < max; i++)
  62. {
  63. int t;
  64.  
  65. if (a[i] <= a[i + 1])
  66. continue;
  67.  
  68. /* swap */
  69. t = a[i];
  70. a[i] = a[i + 1];
  71. a[i + 1] = t;
  72.  
  73. highest = i;
  74. }
  75.  
  76. /* only need to go this high next iteration */
  77. max = highest;
  78. }
  79. while (max > 0);
  80. }
  81.  
  82. /* ----------------------------------------------------------------------- */
  83.  
  84. /* is the given array sorted? (loop variant) */
  85. static int is_ordered__loop(const int *a, int nelems)
  86. {
  87. while (--nelems)
  88. {
  89. if (a[0] > a[1])
  90. return 0; /* not ordered */
  91. a++;
  92. }
  93.  
  94. return 1; /* we ran out of elements: the array must be ordered */
  95. }
  96.  
  97. /* is the given array sorted? (recursive variant) */
  98. static int is_ordered__recursive(const int *a, int nelems)
  99. {
  100. /* we test pairs of numbers. so the final case is when nelems is one, not
  101. * zero */
  102. if (nelems <= 1)
  103. return 1; /* we ran out of elements: the array must be ordered */
  104.  
  105. if (a[0] > a[1])
  106. return 0; /* not ordered */
  107.  
  108. return is_ordered__recursive(a + 1, nelems - 1);
  109. }
  110.  
  111. /* ----------------------------------------------------------------------- */
  112.  
  113. #define MINRAND 0
  114. #define MAXRAND 100
  115. static void generate_random(unsigned int seed, int *a, int n)
  116. {
  117. srand(seed);
  118.  
  119. while (n--)
  120. *a++ = MINRAND + rand() / (RAND_MAX / (MAXRAND - MINRAND + 1) + 1);
  121. }
  122.  
  123. /* ----------------------------------------------------------------------- */
  124.  
  125. #define MAXELEMS 20
  126. #define SEED 0xa8cb8794
  127.  
  128. void test_bubblesort(void)
  129. {
  130. int a[MAXELEMS];
  131. int b[MAXELEMS];
  132. int i;
  133.  
  134. /* first test the looping variant */
  135.  
  136. generate_random(SEED, a, MAXELEMS);
  137. bubblesort__loop(a, MAXELEMS);
  138.  
  139. for (i = 0; i < MAXELEMS - 1; i++)
  140. printf("%d, ", a[i]);
  141. printf("%d\n", a[i]);
  142.  
  143. if (!is_ordered__loop(a, MAXELEMS))
  144. fprintf(stderr, "Failure: Loop results are not ordered!\n");
  145.  
  146. /* now test the recursive variant */
  147.  
  148. generate_random(SEED, b, MAXELEMS);
  149. bubblesort__recursive(b, MAXELEMS);
  150.  
  151. for (i = 0; i < MAXELEMS - 1; i++)
  152. printf("%d, ", b[i]);
  153. printf("%d\n", b[i]);
  154.  
  155. if (!is_ordered__recursive(b, MAXELEMS))
  156. fprintf(stderr, "Failure: Recursive results are not ordered!\n");
  157.  
  158. /* do they match? */
  159.  
  160. if (memcmp(a, b, MAXELEMS * sizeof(a[0])) != 0)
  161. {
  162. fprintf(stderr, "Failure: Sorted results don't match!\n");
  163. return;
  164. }
  165.  
  166. printf("OK!\n");
  167. }
  168.  
  169. int main(void)
  170. {
  171. test_bubblesort();
  172. }
Advertisement
Add Comment
Please, Sign In to add comment