forked from Obelem/sorting_algorithms
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path103-merge_sort.c
85 lines (79 loc) · 1.64 KB
/
103-merge_sort.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "sort.h"
#include <stdlib.h>
#include <stdio.h>
/**
* merge_sort - A function that sorts an array using merge algorithm.
* @array: The array to sort.
* @size: The size of the array.
* Return: Nothing.
*/
void merge_sort(int *array, size_t size)
{
size_t i = 0;
int *base = NULL;
if (array == NULL || size < 2)
return;
base = malloc(sizeof(int) * size);
if (base == NULL)
return;
for (; i < size; i++)
base[i] = array[i];
merge_partition(0, size, array, base);
free(base);
}
/**
* merge - A function that sorts the subarrays.
* @lo: Lower index.
* @mi: Middle index.
* @hi: Higher index.
* @dest: Destination for data.
* @src: Input data.
* Return: Nothing
*/
void merge(size_t lo, size_t mi, size_t hi, int *dest, int *src)
{
size_t i = 0, j = 0, k = 0;
printf("Merging...\n");
printf("[left]: ");
print_array(src + lo, mi - lo);
printf("[right]: ");
print_array(src + mi, hi - mi);
i = lo;
j = mi;
k = lo;
for (; k < hi; k++)
{
if (i < mi && (j >= hi || src[i] <= src[j]))
{
dest[k] = src[i];
i++;
}
else
{
dest[k] = src[j];
j++;
}
}
printf("[Done]: ");
print_array(dest + lo, hi - lo);
}
/**
* merge_partition - A funtion that splits the array recursively.
* @lo: Lower index.
* @hi: Higher index.
* @array: The array to sort.
* @base: The copy of the array.
* Return: Nothing.
*/
void merge_partition(size_t lo, size_t hi, int *array, int *base)
{
size_t mi = 0;
if (hi - lo < 2)
return;
mi = (lo + hi) / 2;
merge_partition(lo, mi, array, base);
merge_partition(mi, hi, array, base);
merge(lo, mi, hi, array, base);
for (mi = lo; mi < hi; mi++)
base[mi] = array[mi];
}