-
Notifications
You must be signed in to change notification settings - Fork 0
/
mergeSortedArray.c
37 lines (35 loc) · 978 Bytes
/
mergeSortedArray.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
#include <stdio.h>
#include <stdlib.h>
int* merge(int left[], int right[], int llen, int rlen) {
int lindex = 0, rindex = 0, index = 0;
int *res = (int *)malloc(sizeof(int) * (llen + rlen));
while(lindex < llen && rindex < rlen) {
if(left[lindex] <= right[rindex]) {
res[index] = left[lindex];
lindex++;
} else {
res[index] = right[rindex];
rindex++;
}
index++;
}
while(lindex < llen) {
res[index++] = left[lindex++];
}
while(rindex < rlen) {
res[index++] = right[rindex++];
}
return res;
}
main()
{
int left[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int right[] = {1, 2, 4, 8, 16, 32, 64, 128};
int llen = sizeof(left)/sizeof(left[0]), rlen = sizeof(right)/sizeof(right[0]);
//int res[17];
int *res = merge(left, right, llen, rlen);
int i;
for(i = 0; i < llen + rlen; i++)
printf("%d ", res[i]);
printf("\n");
}