-
Notifications
You must be signed in to change notification settings - Fork 0
/
103-merge_sort.c
81 lines (68 loc) · 1.96 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
#include "sort.h"
void merge_sub(int *sub, int *buffr, size_t front, size_t mid,
size_t back);
void merge_recursive(int *sub, int *buffr, size_t front, size_t back);
void merge_sort(int *array, size_t size);
/**
* merge_sub - Sort a sub array of integers.
* @sub: sub array of an array of integers to sort.
* @buffr: buffer to store the sorted subarray.
* @front: front index of the array.
* @mid: middle index of the array.
* @back: back index of the array.
*/
void merge_sub(int *sub, int *buffr, size_t front, size_t mid,
size_t back)
{
size_t i, j, k = 0;
printf("Merging...\n[left]: ");
print_array(sub + front, mid - front);
printf("[right]: ");
print_array(sub + mid, back - mid);
for (i = front, j = mid; i < mid && j < back; k++)
buffr[k] = (sub[i] < sub[j]) ? sub[i++] : sub[j++];
for (; i < mid; i++)
buffr[k++] = sub[i];
for (; j < back; j++)
buffr[k++] = sub[j];
for (i = front, k = 0; i < back; i++)
sub[i] = buffr[k++];
printf("[Done]: ");
print_array(sub + front, back - front);
}
/**
* merge_recursive - Implement the merge sort algorithm through recursion.
* @sub: A sub array of an array of integers to sort.
* @buffr: A buffer to store the sorted result.
* @front: The front index of the subarray.
* @back: The back index of the subarray.
*/
void merge_recursive(int *sub, int *buffr, size_t front, size_t back)
{
size_t mid;
if (back - front > 1)
{
mid = front + (back - front) / 2;
merge_recursive(sub, buffr, front, mid);
merge_recursive(sub, buffr, mid, back);
merge_sub(sub, buffr, front, mid, back);
}
}
/**
* merge_sort - Sort an array of integers in ascending
* @array: array of integers.
* @size: size of the array.
*
* Description: an Implements the top-down merge sort algorithm.
*/
void merge_sort(int *array, size_t size)
{
int *buffr;
if (array == NULL || size < 2)
return;
buffr = malloc(sizeof(int) * size);
if (buffr == NULL)
return;
merge_recursive(array, buffr, 0, size);
free(buffr);
}