-
Notifications
You must be signed in to change notification settings - Fork 0
/
103-merge_sort.c
87 lines (70 loc) · 1.58 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
#include "sort.h"
/**
* merge_sort - sorting an array of integers from the smallest to the biggest
* using the Merge sort algorithm
* @array: array to sort
* @size: size of the array
* Return: void
*/
void merge_sort(int *array, size_t size)
{
int *temp = NULL;
if (!array || size < 2)
return;
temp = malloc(sizeof(int) * size);
if (!temp)
return;
merge_sort_rec(array, size, temp);
free(temp);
}
/**
* merge_sort_rec - sorts an array of integers from the smallest
* to the biggest using the Merge sort algorithm
* @array: array to sort
* @size: size of the array
* @temp: tmp array
* Return: void
*/
void merge_sort_rec(int *array, size_t size, int *temp)
{
size_t mid = 0;
if (size < 2)
return;
mid = size / 2;
merge_sort_rec(array, mid, temp);
merge_sort_rec(array + mid, size - mid, temp);
merge(array, size, mid, temp);
}
/**
* merge - sorts an array of integers in ascending order using the
* Merge sort algorithm
* @array: array to sort
* @size: size of the array
* @mid: mid of the array
* @tmp: tmp array
* Return: void
*/
void merge(int *array, size_t size, size_t mid, int *tmp)
{
size_t i = 0, j = mid, k = 0;
printf("Merging...\n");
printf("[left]: ");
print_array(array, mid);
printf("[right]: ");
print_array(array + mid, size - mid);
while (i < mid && j < size)
{
if (array[i] < array[j])
tmp[k++] = array[i++];
else
tmp[k++] = array[j++];
}
while (i < mid)
tmp[k++] = array[i++];
while (j < size)
tmp[k++] = array[j++];
for (i = 0; i < size; i++)
array[i] = tmp[i];
printf("[Done]: ");
print_array(tmp, size);
}