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.

mergeseq.c 796B

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