-
Notifications
You must be signed in to change notification settings - Fork 362
Expand file tree
/
Copy path4mrgSort.cpp
More file actions
87 lines (76 loc) · 1.02 KB
/
4mrgSort.cpp
File metadata and controls
87 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
78
79
80
81
82
83
84
85
86
87
#include <iostream>
using namespace std;
void combine(int a[], int s, int m, int e)
{
int *buffer = new int [e+1];
int k = s;
while(k <= e)
{
buffer[k] = a[k];
k = k + 1;
}
int i = s;
int j = m+1;
k = s;
while(i <= m && j <= e)
{
if(buffer[i] <= buffer[j])
{
a[k] = buffer[i];
i = i + 1;
}
else
{
a[k] = buffer[j];
j = j + 1;
}
k = k + 1;
}
while(i <= m)
{
a[k] = buffer[i];
i = i + 1;
k = k + 1;
}
while(j <= e)
{
a[k] = buffer[j];
j = j + 1;
k = k + 1;
}
delete[] buffer;
}
void mrgSort(int a[], int s, int e)
{
if (s == e)
{
return;
}
int m = (s+e)/2;
mrgSort(a, s, m);
mrgSort(a, m+1, e);
combine(a,s,m,e);
}
void mrgSort(int a[], int n)
{
mrgSort(a,0,n-1);
}
void show(int a[], int n)
{
int i = 0;
while(i < n)
{
cout << a[i] << ", ";
i = i+1;
}
cout << endl;
}
int main()
{
int arr[] = {22,66,44,27,967,34,2,90,4567,21,75,9,44};
int arrSize = sizeof(arr)/sizeof(int);
show(arr, arrSize);
mrgSort(arr, arrSize);
show(arr, arrSize);
return 0;
}