-
Notifications
You must be signed in to change notification settings - Fork 1
/
103-merge_sort_test.c
109 lines (100 loc) · 2.2 KB
/
103-merge_sort_test.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
#include "sort.h"
/**
* merge_sort - Sorts an array of integers in ascending order
* using the Merge sort algorithm
*
* @array: Pointer to array
* @size: length of the array
*
* Return: Nothing
*/
void merge_sort(int *array, size_t size)
{
int n = (int)size;
if (array == NULL || size < 2)
return;
merge_recursion(array, n);
}
/**
* merge_sort - Sorts an array of integers in ascending order
* using the Merge sort algorithm
*
* @array: Pointer to array
* @size: length of the array
*
* Return: Nothing
*/
void merge_recursion(int *half, int n)
{
int m = n / 2, index = 0;
int *left = NULL, *right = NULL;
printf("entre en el merge\n");
if (n == 1)
return;
if (m % 2 == 0)
{
printf("entre en el m 2 = 0\n");
left = malloc((sizeof(int)) * n + 1);
printf("malloc m2 = 0 left");
right = malloc((m * sizeof(int)) * m + 1);
printf("malloc m2 = 0 left");
if (half == NULL || right == NULL)
return;
left = place_elements(half, left, index, m);
right = place_elements(half, right, m + 1, n);
merge_recursion(left, m);
merge_recursion(right, m);
}
else
{
left = malloc(m * sizeof(int) + 1);
right = malloc(m + 1 * sizeof(int) + 1);
if (half == NULL || right == NULL)
return;
left = place_elements(half, left, index, m);
right = place_elements(half, right, m - 1, n);
merge_recursion(left, m);
merge_recursion(right, m + 1);
}
new_array(half, left, m);
new_array(right, right, m);
}
/**
* merge_sort - Sorts an array of integers in ascending order
* using the Merge sort algorithm
*
* @array: Pointer to array
* @size: length of the array
*
* Return: Nothing
*/
int *place_elements(int *half, int *new_array, int start, int end)
{
int i = 0;
for (i = start; i <= end; i++)
new_array[i] = half[i];
return new_array;
}
/**
* merge_sort - Sorts an array of integers in ascending order
* using the Merge sort algorithm
*
* @array: Pointer to array
* @size: length of the array
*
* Return: Nothing
*/
void new_array(int *half, int *new_array, int m)
{
int i, tmp;
for (i = 0; i < m; i++)
{
if (new_array[i] > new_array[i + 1])
{
tmp = new_array[i + 1];
new_array[i + 1] = new_array[i];
new_array[1] = tmp;
}
half[i] = new_array[i];
}
}