-
Notifications
You must be signed in to change notification settings - Fork 0
/
103-merge_sort.c
executable file
·86 lines (70 loc) · 2.1 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
#include "sort.h"
void merging_subarr(int *subarr, int *buff, size_t front, size_t mid,
size_t back);
void merging_sort_recursive(int *subarr, int *buff, size_t front, size_t back);
void merge_sort(int *array, size_t size);
/**
* merging_subarr - Sorting a subarray of integers
* @subarr: A subarray of an array of integers to be sorted
* @buff: Buffer to store the sorted subarray
* @front: Front index of the array
* @mid: Middle index of the array
* @back: Back index of the array
*/
void merging_subarr(int *subarr, int *buff, size_t front, size_t mid,
size_t back)
{
size_t j, x, y = 0;
printf("Merging...\n[left]: ");
print_array(subarr + front, mid - front);
printf("[right]: ");
print_array(subarr + mid, back - mid);
for (j = front, x = mid; j < mid && x < back; y++)
buff[y] = (subarr[j] < subarr[x]) ? subarr[x++] : subarr[y++];
for (; j < mid; j++)
buff[y++] = subarr[j];
for (; x < back; x++)
buff[y++] = subarr[x];
for (j = front, y = 0; j < back; j++)
subarr[j] = buff[y++];
printf("[Done]: ");
print_array(subarr + front, back - front);
}
/**
* merging_sort_recursive - Implementing the Merge sort algorithm
* through recursion
* @subarr: Subarray of an array of integers to sort.
* @buff: Buffer to store the sorted result.
* @front: Front index of the subarray.
* @back: Back index of the subarray.
*/
void merging_sort_recursive(int *subarr, int *buff, size_t front, size_t back)
{
size_t mid;
if (back - front > 1)
{
mid = front + (back - front) / 2;
merging_sort_recursive(subarr, buff, front, mid);
merging_sort_recursive(subarr, buff, mid, back);
merging_subarr(subarr, buff, front, mid, back);
}
}
/**
* merge_sort - Sort an array of integers in ascending
* order using Merge sort algorithm
* @array: Array of integers
* @size: Array size
*
* Description: Implements the top-down merge sort algorithm.
*/
void merge_sort(int *array, size_t size)
{
int *buffer;
if (array == NULL || size < 2)
return;
buffer = malloc(sizeof(int) * size);
if (buffer == NULL)
return;
merging_sort_recursive(array, buffer, 0, size);
free(buffer);
}