-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmergeSort.c
87 lines (63 loc) · 1.08 KB
/
mergeSort.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 <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
int main()
{
int size1,size2;
int i;
int array1[100];
int array2[100];
int final[200];
printf("ENTER THE NUMBER OF ELEMENTS IN THE 1st ARRAY:\n");
scanf("%i",&size1);
printf("ENTER THE ELEMENTS:\n");
for(i=0;i<size1;++i)
{
scanf("%i",&array1[i]);
}
printf("ENTER THE NUMBER OF ELEMENTS IN THE 2nd ARRAY:\n");
scanf("%i",&size2);
printf("ENTER THE ELEMENTS:\n");
for(i=0;i<size2;++i)
{
scanf("%i",&array2[i]);
}
//MERGING THE 2 ARRAYS
int x=0,y=0,j,k;
for(i=0;i<(size1+size2);++i)
{
if(x > (size1-1))//EXHAUSTION OF 1st ARRAY
{
for(j=i;j<(size1+size2);++j)
{
final[j] = array2[y];
++y;
}
break;
}
else if(y > (size2-1))//EXHAUSTION OF 2nd ARRAY
{
for(k=i;k<(size1+size2);++k)
{
final[k] = array1[x];
++x;
}
break;
}
else if(array1[x] < array2[y])
{
final[i] = array1[x];
++x;
}
else
{
final[i] = array2[y];
++y;
}
}
printf("MERGED AND SORTED ARRAY:\n");
for(i=0;i<(size1+size2);++i)
{
printf("%i\n",final[i]);
}
}