-
Notifications
You must be signed in to change notification settings - Fork 0
/
103-merge_sort.c
79 lines (66 loc) · 1.64 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
#include "sort.h"
/**
* merge_sub - merges subarrays
* @arr: allocated memory for arrays
* @array: array to merge
* @low: index of the left most element
* @pivot: index of the middle element
* @high: index of the right most element
*/
void merge_subarray(int *arr, int *array, size_t low,
size_t pivot, size_t high)
{
size_t i, j, k = 0;
printf("Merging...\n");
printf("[left]: ");
print_array(array + low, pivot - low);
printf("[right]: ");
print_array(array + pivot, high - pivot);
for (i = low, j = pivot; i < pivot && j < high; k++)
{
if (array[i] < array[j])
arr[k] = array[i++];
else
arr[k] = array[j++];
}
while (i < pivot)
arr[k++] = array[i++];
while (j < high)
arr[k++] = array[j++];
for (k = low, i = 0; k < high; k++)
array[k] = arr[i++];
printf("[Done]: ");
print_array(array + low, high - low);
}
/**
* merge_recursion - function that sorts an array using merge sort
* @arr: allocated memory to copy array
* @array: array to sort
* @low: index of the left most element
* @high: index of the right most element
*/
void merge_recursion(int *arr, int *array, size_t low, size_t high)
{
size_t pivot;
if (high - low > 1)
{
pivot = (high - low) / 2 + low;
merge_recursion(arr, array, low, pivot);
merge_recursion(arr, array, pivot, high);
merge_subarray(arr, array, low, pivot, high);
}
}
/**
* merge_sort - sorts an array with the Merge Sort algorithm
* @array: array of ints to sort
* @size: size of the array
*/
void merge_sort(int *array, size_t size)
{
int *arr;
if (!array || size < 2)
return;
arr = malloc(sizeof(int) * size);
merge_recursion(arr, array, 0, size);
free(arr);
}