-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmergesort.cpp
More file actions
70 lines (68 loc) · 1.69 KB
/
Copy pathmergesort.cpp
File metadata and controls
70 lines (68 loc) · 1.69 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
#include <iostream>
#include <set>
using namespace std;
// mergesort, but written in an iterative way
template <typename T>
void mergesort(T* a, size_t len, T* buf)
{
for (size_t w = 1; w < len; w *= 2)
{
for (size_t i = 0; i + w < len; i += w+w)
{
// merge
size_t j1 = i;
size_t j2 = i + w;
size_t p = 0;
while (j1 < i + w && j2 < i + w+w && j2 < len)
if (a[j1] < a[j2])
buf[p++] = a[j1++];
else
buf[p++] = a[j2++];
while (j1 < i + w)
buf[p++] = a[j1++];
while (j2 < i + w*w && j2 < len)
buf[p++] = a[j2++];
// copy back to the original array
for (size_t j = 0; j < p; j++)
a[i+j] = buf[j];
}
}
}
int main()
{
constexpr size_t MAXLEN = 100;
pair<int,int> a[MAXLEN], buf[MAXLEN];
set<int> st;
cout << "Enter array length: ";
size_t len;
cin >> len;
if (len > MAXLEN)
{
cout << "length too big" << endl;
return 1;
}
cout << "Enter array: ";
for (int i = 0; i < len; i++)
{
int x;
cin >> x;
a[i].first = x;
a[i].second = st.count(x);
st.insert(x);
}
cout << "Before sort: ";
for (int i = 0; i < len; i++)
{
if (i > 0) cout << " ";
cout << "(" << a[i].first << "," << a[i].second << ")";
}
mergesort(a, len, buf);
cout << endl << "After sort: ";
for (int i = 0; i < len; i++)
{
if (i > 0) cout << " ";
cout << "(" << a[i].first << "," << a[i].second << ")";
}
cout << endl;
return 0;
}