Skip to content

Commit 25835c3

Browse files
committed
feat: implement merge sort algorithm with array of ints
1 parent bfb97a9 commit 25835c3

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

103-O

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
O(nlog(n))
2+
O(nlog(n))
3+
O(nlog(n))

103-merge_sort.c

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#include "sort.h"
2+
#include <stdlib.h>
3+
4+
/**
5+
* merge_sort - Implements the merge sort algorithm using an array of ints
6+
* @array: Array of ints
7+
* @size: Length of the array
8+
*/
9+
void merge_sort(int *array, size_t size)
10+
{
11+
int *temp_array = NULL;
12+
13+
if (!array || size < 2)
14+
return;
15+
16+
temp_array = malloc(sizeof(int) * size);
17+
if (!temp_array)
18+
return;
19+
20+
copy_array(array, temp_array, size);
21+
split(temp_array, 0, size, array);
22+
free(temp_array);
23+
}
24+
25+
/**
26+
* copy_array - Copies the integers in @array of size @size to @temp_array
27+
* @array: Source array
28+
* @temp_array: Destination array
29+
* @size: Length of @array
30+
*/
31+
void copy_array(int *array, int *temp_array, size_t size)
32+
{
33+
size_t idx;
34+
35+
for (idx = 0; idx < size; idx++)
36+
temp_array[idx] = array[idx];
37+
}
38+
39+
/**
40+
* split - Divides an array into two until each array has one integer only
41+
* @temp_array: Temporary array for working on the sort
42+
* @beg: Beginning index in the array
43+
* @end: Ending index in the array
44+
* @array: Main array
45+
*/
46+
void split(int *temp_array, size_t beg, size_t end, int *array)
47+
{
48+
size_t mid = 0;
49+
50+
if (end - beg < 2)
51+
return;
52+
53+
mid = (end + beg) / 2;
54+
55+
split(array, beg, mid, temp_array);
56+
split(array, mid, end, temp_array);
57+
58+
merge(temp_array, beg, mid, end, array);
59+
}
60+
61+
/**
62+
* merge - Merges integers in two array in a sorted order
63+
* @array: Second array
64+
* @beg: Start index in the array
65+
* @mid: Middle index in the array
66+
* @end: End index in the array
67+
* @temp_array: First array
68+
*/
69+
void merge(int *array, size_t beg, size_t mid, size_t end, int *temp_array)
70+
{
71+
size_t i = beg, j = mid, k;
72+
73+
printf("Merging...\n");
74+
printf("[left]: ");
75+
print_array(array + beg, mid - beg);
76+
printf("[right]: ");
77+
print_array(array + mid, end - mid);
78+
79+
for (k = beg; k < end; k++)
80+
{
81+
if (i < mid && (j >= end || array[i] <= array[j]))
82+
temp_array[k] = array[i], i++;
83+
else
84+
temp_array[k] = array[j], j++;
85+
}
86+
87+
printf("[Done]: ");
88+
print_array(temp_array + beg, end - beg);
89+
}

sort.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ void counting_sort(int *array, size_t size);
100100
int find_max(int *array, size_t size);
101101
void sort(int *array, int *count, int *sorted_array, size_t size);
102102

103+
void merge_sort(int *array, size_t size);
104+
void copy_array(int *array, int *temp_array, size_t size);
105+
void split(int *temp_array, size_t beg, size_t end, int *array);
106+
void merge(int *array, size_t beg, size_t mid, size_t end, int *temp_array);
107+
103108
/* void sort_list_of_two_and_print(listint_t **list); */
104109
/* void sort_two_only(listint_t **list); */
105110
/* int sort_for_last_node(listint_t *node_to_insert); */

0 commit comments

Comments
 (0)