diff --git a/sort.c b/sort.c index 88e0663..ca0b177 100644 --- a/sort.c +++ b/sort.c @@ -76,28 +76,53 @@ bubble_sort(int *arr, size_t sz) static void _merge(int *tmp, int *a, int *b, int *end) { - int *left = a; - int *right = b; - int *result = tmp; - - - while (left < b && right < end) { - if (*left <= *right) { - *result++ = *left++; - } else { - *result++ = *right++; +// int *left = a; +// int *right = b; +// int *result = tmp; +// +// +// while (left < b && right < end) { +// if (*left <= *right) { +// *result++ = *left++; +// } else { +// *result++ = *right++; +// } +// } +// +// +// while (left < b) { +// *result++ = *left++; +// } +// +// +// while (right < end) { +// *result++ = *right++; +// } + int a1 = 0, b1 = 0, tmp1 = 0; + int ra = b - a, rb = end - b; + while (ra > a1 && rb > b1) + { + if (a[a1] < b[b1]) + { + tmp[tmp1++] = a[a1++]; + } + else + { + tmp[tmp1++] = b[b1++]; } } - - - while (left < b) { - *result++ = *left++; - } - - - while (right < end) { - *result++ = *right++; + while (ra != a1) + { + tmp[tmp1] = a[a1]; + a1++; + tmp1++; } + while (rb != b1) + { + tmp[tmp1] = b[b1]; + b1++; + tmp1++; + } } static void