-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path103-merge_sort.c
145 lines (122 loc) · 2.65 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <stdio.h>
#include <stdlib.h>
#include "sort.h"
/**
* copy - copies data from one buffer to another
*
* @src: source buffer
* @dst: destination buffer
* @size: size of buffers
*
* Return: No Return
*/
void copy(int *src, int *dst, int size)
{
int i;
for (i = 0; i < size; i++)
dst[i] = src[i];
}
/**
* merge - merges two sets of data in ascending order
* but they must already be sorted before hand
* @array: first set of data
* @buff: second set of data
* @minL: lower range of first set of data
* @maxL: upper range of first set of data
* @minR: lower range of second set of data
* @maxR: upper range of second set of data
*
* Return: No Return
*/
void merge(int *array, int *buff, int minL, int maxL, int minR, int maxR)
{
int i = minL, j = minR, k = minL;
while (i <= maxL || j <= maxR)
if (i <= maxL && j <= maxR)
if (buff[i] <= buff[j])
array[k] = buff[i], k++, i++;
else
array[k] = buff[j], k++, j++;
else if (i > maxL && j <= maxR)
array[k] = buff[j], k++, j++;
else
array[k] = buff[i], k++, i++;
}
/**
* printcheck - prints an array in a given range
*
* @array: array of data to be print
* @r1: start of range
* @r2: end of range
*
* Return: No Return
*/
void printcheck(int *array, int r1, int r2)
{
int i;
for (i = r1; i <= r2; i++)
{
if (i > r1)
printf(", ");
printf("%d", array[i]);
}
printf("\n");
}
/**
* split - recursive function to split data into merge tree
*
* @array: array of data to be split
* @buff: auxiliary array of data for merging
* @min: min range of data in array
* @max: max range of data in array
* @size: size of total data
*
* Return: No Return
*/
void split(int *array, int *buff, int min, int max, int size)
{
int mid, tmax, minL, maxL, minR, maxR;
if ((max - min) <= 0)
return;
mid = (max + min + 1) / 2;
tmax = max;
max = mid - 1;
minL = min;
maxL = max;
split(array, buff, min, max, size);
min = mid;
max = tmax;
minR = min;
maxR = max;
split(array, buff, min, max, size);
printf("Merging...\n");
printf("[left]: ");
printcheck(array, minL, maxL);
printf("[right]: ");
printcheck(array, minR, maxR);
merge(array, buff, minL, maxL, minR, maxR);
copy(array, buff, size);
printf("[Done]: ");
printcheck(array, minL, maxR);
}
/**
* merge_sort - sorts an array of integers in ascending order
* using the Merge sort algorithm
*
* @array: array of data to be sorted
* @size: size of data
*
* Return: No Return
*/
void merge_sort(int *array, size_t size)
{
int *buff;
if (size < 2)
return;
buff = malloc(sizeof(int) * size);
if (buff == NULL)
return;
copy(array, buff, size);
split(array, buff, 0, size - 1, size);
free(buff);
}