-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMerge.cpp
More file actions
77 lines (76 loc) · 1.02 KB
/
Merge.cpp
File metadata and controls
77 lines (76 loc) · 1.02 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
#include<iostream>
#include<conio.h>
using namespace std;
void merge(int a[], int lb, int mid, int ub)
{
int i = lb, b[20], k = lb;
int j = mid + 1;
while (i<=mid && j <= ub)
{
if (a[i] <= a[j])
{
b[k] = a[i];
i++;
k++;
}
else
{
b[k] = a[j];
j++;
k++;
}
}
if (i > mid)
{
while (j <= ub)
{
b[k] = a[j];
k++;
j++;
}
}
else
{
while (i <= mid)
{
b[k] = a[i];
i++;
k++;
}
}
for (int i = lb; i <= ub; i++)
{
a[i] = b[i];
}
}
void sort(int a[], int lb, int ub)
{
int mid;
if (lb < ub)
{
mid = (lb + ub) / 2;
sort(a, lb, mid);
sort(a, mid + 1, ub);
merge(a, lb, mid, ub);
}
}
void main()
{
int a[20], lb, ub, n;
cout << "\nEnter the size of array ";
cin >> n;
for (int i = 0; i < n; i++)
{
cout << "Enter the values of array " << i + 1<<": ";
cin >> a[i];
}
lb = 0;
ub = n - 1;
sort(a, lb, ub);
for (int i = 0; i < n; i++)
{
cout << a[i]<<" ";
}
_getch();
return;
}