-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path103-merge_sort.c
More file actions
84 lines (75 loc) · 1.85 KB
/
103-merge_sort.c
File metadata and controls
84 lines (75 loc) · 1.85 KB
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
#include "sort.h"
/**
* merge - Merges the splits from merge_sorty
* @array: Array split to merge
* @low: lowest index of split
* @middle: middle index of split
* @high: high index of split
* @temp: temp array for merging
*/
void merge(int *array, int low, int middle, int high, int *temp)
{
int i, j, k, l = 0, r = 0, n, left[4096], right[4096];
printf("Merging...\n");
i = low, j = middle + 1, k = l = 0;
while (i <= middle && j <= high)
{
if (array[i] <= array[j])
temp[k] = left[l] = array[i], k++, i++, l++;
else
temp[k] = right[r] = array[j], k++, j++, r++;
}
while (i <= middle)
temp[k] = left[l] = array[i], k++, i++, l++;
while (j <= high)
temp[k] = right[r] = array[j], k++, j++, r++;
printf("[left]: ");
for (n = 0; n < l; n++)
(n == 0) ? printf("%d", left[n]) : printf(", %d", left[n]);
printf("\n[right]: ");
for (n = 0; n < r; n++)
(n == 0) ? printf("%d", right[n]) : printf(", %d", right[n]);
printf("\n[Done]: ");
for (i = low; i <= high; i++)
{
array[i] = temp[i - low], printf("%d", array[i]);
if (i != high)
printf(", ");
else
printf("\n");
}
}
/**
* merge_sorty - recurrsive function utilizing merge sort algo
* @array: Array
* @low: Lowest index of split
* @high: highest index of split
* @temp: temp array for mergin
*/
void merge_sorty(int *array, int low, int high, int *temp)
{
int middle;
if (low < high)
{
middle = ((high + low - 1) / 2);
merge_sorty(array, low, middle, temp);
merge_sorty(array, middle + 1, high, temp);
merge(array, low, middle, high, temp);
}
}
/**
* merge_sort - Sorts array with merge sort algo
* @array: array to sort
* @size: Size of array to sort
*/
void merge_sort(int *array, size_t size)
{
int *temp;
if (array == NULL || size < 2)
return;
temp = malloc(sizeof(int) * (size + 1));
if (temp == NULL)
return;
merge_sorty(array, 0, size - 1, temp);
free(temp);
}