diff --git a/sort.c b/sort.c index b43ee6e..88e0663 100644 --- a/sort.c +++ b/sort.c @@ -21,7 +21,16 @@ typedef struct { void insert_sort(int *arr, size_t sz) { - // + int i, j; + for (i = 1; i < sz; i++){ + int key = arr[i]; + j = i - 1; + while (j >= 0 && arr[j] > key){ + arr[j + 1] = arr[j]; + j--; + } + arr[j + 1] = key; + } } void @@ -67,7 +76,28 @@ 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++; + } + } + + + while (left < b) { + *result++ = *left++; + } + + + while (right < end) { + *result++ = *right++; + } } static void