-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path103-merge_sort.c
89 lines (74 loc) · 1.91 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
#include "sort.h"
#include <stdlib.h>
/**
* merge_sort - Implements the merge sort algorithm using an array of ints
* @array: Array of ints
* @size: Length of the array
*/
void merge_sort(int *array, size_t size)
{
int *temp_array = NULL;
if (!array || size < 2)
return;
temp_array = malloc(sizeof(int) * size);
if (!temp_array)
return;
copy_array(array, temp_array, size);
split(temp_array, 0, size, array);
free(temp_array);
}
/**
* copy_array - Copies the integers in @array of size @size to @temp_array
* @array: Source array
* @temp_array: Destination array
* @size: Length of @array
*/
void copy_array(int *array, int *temp_array, size_t size)
{
size_t idx;
for (idx = 0; idx < size; idx++)
temp_array[idx] = array[idx];
}
/**
* split - Divides an array into two until each array has one integer only
* @temp_array: Temporary array for working on the sort
* @beg: Beginning index in the array
* @end: Ending index in the array
* @array: Main array
*/
void split(int *temp_array, size_t beg, size_t end, int *array)
{
size_t mid = 0;
if (end - beg < 2)
return;
mid = (end + beg) / 2;
split(array, beg, mid, temp_array);
split(array, mid, end, temp_array);
merge(temp_array, beg, mid, end, array);
}
/**
* merge - Merges integers in two array in a sorted order
* @array: Second array
* @beg: Start index in the array
* @mid: Middle index in the array
* @end: End index in the array
* @temp_array: First array
*/
void merge(int *array, size_t beg, size_t mid, size_t end, int *temp_array)
{
size_t i = beg, j = mid, k;
printf("Merging...\n");
printf("[left]: ");
print_array(array + beg, mid - beg);
printf("[right]: ");
print_array(array + mid, end - mid);
for (k = beg; k < end; k++)
{
if (i < mid && (j >= end || array[i] <= array[j]))
temp_array[k] = array[i], i++;
else
temp_array[k] = array[j], j++;
}
printf("[Done]: ");
print_array(temp_array + beg, end - beg);
}