-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path103-merge_sort.c
More file actions
100 lines (88 loc) · 1.81 KB
/
Copy path103-merge_sort.c
File metadata and controls
100 lines (88 loc) · 1.81 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "sort.h"
/**
* merge_sort - sorts an array of integers in ascending order using
* the Merge sort algorithm
*
* @array: array to be sorted.
* @size: size of the array.
*/
void merge_sort(int *array, size_t size)
{
int *B;
if (array == NULL || size < 2)
return;
B = malloc(sizeof(int) * size);
if (B == NULL)
return;
copy_array(array, B, size);
top_down_split_merge(array, B, 0, size);
copy_array(B, array, size);
free(B);
}
/**
* copy_array - copies one array to another.
*
* @A: first array to copy from.
* @B: second array to copy to.
* @size: size of the array.
*/
void copy_array(int *A, int *B, size_t size)
{
size_t i;
for (i = 0; i < size; ++i)
B[i] = A[i];
}
/**
* top_down_split_merge - split the array.
*
* @A: first array.
* @B: second array.
* @iBegin: the beginning.
* @iEnd: the end.
*/
void top_down_split_merge(int *A, int *B, size_t iBegin, size_t iEnd)
{
size_t iMiddle;
if (iEnd - iBegin <= 1)
return;
iMiddle = (iBegin + iEnd) / 2;
top_down_split_merge(A, B, iBegin, iMiddle);
top_down_split_merge(A, B, iMiddle, iEnd);
printf("Merging...\n");
printf("[left]: ");
print_array(B + iBegin, iMiddle - iBegin);
printf("[right]: ");
print_array(B + iMiddle, iEnd - iMiddle);
top_down_merge(A, B, iBegin, iMiddle, iEnd);
printf("[Done]: ");
print_array(B + iBegin, iEnd - iBegin);
}
/**
* top_down_merge - sort the array.
*
* @A: first array.
* @B: second array.
* @iBegin: the begging.
* @iMiddle: the middle.
* @iEnd: the end.
*/
void top_down_merge(int *A, int *B, size_t iBegin, size_t iMiddle,
size_t iEnd)
{
size_t i = iBegin, j = iMiddle, k;
for (k = iBegin; k < iEnd; ++k)
{
if (i < iMiddle && (j >= iEnd || A[i] <= A[j]))
{
B[k] = A[i];
i++;
}
else
{
B[k] = A[j];
j++;
}
}
for (k = iBegin; k < iEnd; ++k)
A[k] = B[k];
}