-
Notifications
You must be signed in to change notification settings - Fork 0
/
103-merge_sort.c
87 lines (77 loc) · 2.24 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 - the merge sort algorithm's interface
* @array: array of integers to be sorted
* @size: size of the array
*
* Return: void
*/
void merge_sort(int *array, size_t size)
{
int *temp; /* temporaray array for staging */
temp = malloc(sizeof(int) * size);
if (temp == NULL || array == NULL || size < 2)
return;
mergesrt(array, 0, size, temp);
free(temp);
}
/**
* mergesrt - the merge sort algorithm implementation
* @A: array of integers to be sorted
* @left: the start of the array at current state,
* the position of the first element itself
* @right: the end of the array at current state,
* the position of the last element itself
* @temp: temporary array
*
* Return: void
*/
void mergesrt(int A[], int left, int right, int *temp)
{
int mid;
if (right - left > 1)
{
mid = left + (right - left) / 2;
/* above avoids integer overflow for large integers */
mergesrt(A, left, mid, temp);
mergesrt(A, mid, right, temp);
merge(A, left, mid, right, temp);
}
}
/**
* merge - the merger function for mergesrt
* @A: array of integers to be sorted
* @left: the start of the array at current state,
* the position of the first element itself
* @right: the end of the array at current state,
* the position of the last element itself
* @mid: the middle of the current array,
* size of the left array is less than or equal to that of the right array
* @temp: temporary array
*
* Return: void
*/
void merge(int A[], int left, int mid, int right, int *temp)
{
int i = left; /* counter for - left array*/
int j = mid; /* counter for - right array*/
int k = 0; /* counter for temp array */
printf("Merging...\n[left]: ");
print_array(A + left, mid - left);
printf("[right]: ");
print_array(A + mid, right - mid);
/* copy from both left and rigth arrays but in a sorted manner */
while (i < mid && j < right)
temp[k++] = (A[i] < A[j]) ? A[i++] : A[j++];
/* copy left-overs of left array if any */
while (i < mid)
temp[k++] = A[i++];
/* copy left-overs of right array if any */
while (j < right)
temp[k++] = A[j++];
/* move sorted data from the staging area to the main array */
for (k = 0, i = left; i < right;)
A[i++] = temp[k++];
printf("[Done]: ");
print_array(A + left, right - left);
}