-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path103-merge_sort.c
121 lines (104 loc) · 1.94 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "sort.h"
/**
* print_merge - prints the subarray during the merge process
*
* @msg: message indicating the purpose of the printed subarray
* @array: the array or subarray to print
* @from: The starting index of the subarray
* @to: The ending index of the subarray
*/
void print_merge(char *msg, int *array, int from, int to)
{
char *sep;
int i;
printf("[%s]: ", msg);
sep = "";
for (i = from; i <= to; i++)
{
printf("%s%d", sep, array[i]);
sep = ", ";
}
printf("\n");
}
/**
* merge - updates the original array after sorting and merging the
* two sub-arrays
*
* @array: original array
* @lb: lower bound
* @mid: the middle that seperates the two sub-arrays to merge
* @ub: upper bound
* @b: temporary array
*/
void merge(int *array, int lb, int mid, int ub, int *b)
{
int i = lb, j = mid + 1, k = lb;
printf("Merging...\n");
print_merge("left", array, lb, mid);
print_merge("right", array, mid + 1, ub);
while (i <= mid && j <= ub)
{
if (array[i] <= array[j])
{
b[k] = array[i];
i++;
}
else
{
b[k] = array[j];
j++;
}
k++;
}
while (i <= mid)
{
b[k] = array[i];
i++;
k++;
}
while (j <= ub)
{
b[k] = array[j];
j++;
k++;
}
for (i = lb; i <= ub; i++)
array[i] = b[i];
print_merge("Done", array, lb, ub);
}
/**
* sort - sorts the array with recursive calls
*
* @array: array to sort
* @lb: lower bound
* @ub: upper bound
* @b: temporary array
*/
void sort(int *array, int lb, int ub, int *b)
{
int mid;
if (lb < ub)
{
mid = (ub + lb - 1) / 2;
sort(array, lb, mid, b);
sort(array, mid + 1, ub, b);
merge(array, lb, mid, ub, b);
}
}
/**
* merge_sort - sorts an array of integers in ascending order using
* the Merge sort algorithm
*
* @array: the array to sort
* @size: the size of the array
*/
void merge_sort(int *array, size_t size)
{
int lb, ub;
int *b;
lb = 0;
ub = size - 1;
b = malloc(sizeof(int) * size);
sort(array, lb, ub, b);
free(b);
}