forked from 43215-Anuj/data-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMerge.cpp
More file actions
105 lines (98 loc) · 1.43 KB
/
Copy pathMerge.cpp
File metadata and controls
105 lines (98 loc) · 1.43 KB
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
#include<iostream>
using namespace std;
template <class t>
class sort
{
t a[10];
public:
void get(int);
void merge(int,int);
void mergesort(int,int,int);
void display(int);
};
template <class t>
void sort <t>::get(int n)
{
int i;
cout<<“\n\n Enter the array elements:”;
for(i=1;i<=n;i++)
cin>>a[i];
}
template <class t>
void sort <t>::display(int n)
{
int i;
cout<<“\n The sorted array is\n”;
for(i=1;i<=n;i++)
cout<<a[i]<<setw(5);
}
template <class t>
void sort <t>::merge(int low,int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
merge(low,mid);
merge(mid+1,high);
mergesort(low,mid,high);
}
}
template <class t>
void sort<t>::mergesort(int low,int mid,int high)
{
t b[10];
int h,i,j,k;
h=low;
i=low;
j=mid+1;
while((h<=mid)&&(j<=high))
{
if(a[h]<=a[j])
{
b[i]=a[h];
h=h+1;
}
else
{
b[i]=a[j];
j=j+1;
}
i=i+1;
}
if(h>mid)
{
for(k=j;k<=high;k++)
{
b[i]=a[k];
i=i+1;
}
}
else
{
for(k=h;k<=mid;k++)
{
b[i]=a[k];
i=i+1;
}
}
for(k=low;k<=high;k++)
a[k]=b[k];
}
int main()
{
int n;
cout<<“\n\t\t MERGE SORT USING TEMPLATES”;
cout<<“\n\t\t ~~~~~ ~~~~ ~~~~~ ~~~~~~~~~”;
sort<int>n1;
sort<float>n2;
cout<<“\n Enter the array size:”;
cin>>n;
n1.get(n);
n1.merge(1,n);
n1.display(n);
n2.get(n);
n2.merge(1,n);
n2.display(n);
return 0;
}