-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharraymerge.c
114 lines (101 loc) · 1.6 KB
/
arraymerge.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*** Merging of two arrays in sorted form ***/
/*** Author: Sumit Kumar ***/
#include "stdio.h"
#include "malloc.h"
#include "stdlib.h"
int *arr;
int* create(int);
void sort(int *,int);
void display(int *,int);
int* merge(int *,int *,int,int);
int main()
{
int s1,s2;
int *a,*b,*c;
printf("enter the size of array1\n");
scanf("%d",&s1);
printf("enter elements for first array\n");
a=create(s1);
printf("enter the size of second array2\n");
scanf("%d",&s2);
printf("enter elemets for second array\n");
b=create(s2);
sort(a,s1);
sort(b,s1);
printf("first array\n");
display(a,s1);
printf("second array\n");
display(b,s2);
printf("after merging\n");
c=merge(a,b,s1,s2);
display(c,s1+s2);
return 0;
}
int* create(int size)
{
int *arr,i;
arr=(int *)malloc(sizeof(int)* size);
for(i=0;i<size;i++)
{
printf("enter the element number %d\n",i+1);
scanf("%d",&arr[i]);
}
printf("\n");
return arr;
}
void sort(int *arr,int size)
{
int temp,i,j;
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
}
void display(int *arr,int size)
{
int i;
for(i=0;i<size;i++)
{
printf("%d\t",arr[i]);
}
printf("\n");
}
int* merge(int *a,int *b,int s1,int s2)
{
int i,j,k;
int *arr;
int size=s1+s2;
arr=(int *)malloc(sizeof(int)* (size));
for(k=0,j=0,i=0;i<=size;i++)
{
if(a[k]<b[j])
{
arr[i]=a[k];
k++;
if(k>=s1)
{
for(i++;j<s2;j++,i++)
arr[i]=b[j];
}
}
else
{
arr[i]=b[j];
j++;
if(j>=s2)
{
for(i++;k<s1;k++,i++)
arr[i]=a[k];
}
}
}
return arr;
}