You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

d2s.c 813B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #include "d2s.h"
  2. #include "common.h"
  3. void merge_sort_merge_seq(int* array, unsigned l, unsigned m, unsigned h)
  4. {
  5. unsigned i = l, j = m + 1, k = 0;
  6. int tmp[h - l + 1];
  7. while (i <= m && j <= h) {
  8. if (array[i] <= array[j]) {
  9. tmp[k++] = array[i++];
  10. }
  11. else {
  12. tmp[k++] = array[j++];
  13. }
  14. }
  15. while (i <= m) {
  16. tmp[k++] = array[i++];
  17. }
  18. while (j <= h) {
  19. tmp[k++] = array[j++];
  20. }
  21. for (--k; k != (unsigned)-1; --k) {
  22. array[l + k] = tmp[k];
  23. }
  24. }
  25. void merge_sort_sub_seq(int* array, unsigned l, unsigned h)
  26. {
  27. if (l < h) {
  28. unsigned m = (l + h) / 2;
  29. merge_sort_sub_seq(array, l, m);
  30. merge_sort_sub_seq(array, m + 1, h);
  31. merge_sort_merge_seq(array, l, m, h);
  32. }
  33. }
  34. void merge_sort_seq(int* array, unsigned s)
  35. {
  36. merge_sort_sub_seq(array, 0, s - 1);
  37. }