-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistSort.cpp
171 lines (133 loc) · 3.2 KB
/
listSort.cpp
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <iostream>
#include <memory>
template <typename T>
class Node{
public:
T val;
std::shared_ptr<Node<T>> next;
Node() {}
Node(T val) : val(val) {}
};
template <typename T>
class List{
public:
List() {
first = std::make_shared<Node<T>>();
last = first;
sz = 0;
}
void push_back(int val) {
last->val = val;
last->next = std::make_shared<Node<T>>();
last = last->next;
++sz;
}
size_t size() const {
return sz;
}
bool empty() const {
return size() == 0;
}
List<T> splitBy(int ind) {
List<T> rightPart;
int i = 0;
for (auto it = begin(); it != end(); ++it, ++i) {
if (i + 1 == ind) {
rightPart.first = it.getNode()->next;
rightPart.last = last;
rightPart.sz = size() - ind;
sz = ind;
last = std::make_shared<Node<T>>();
it.getNode()->next = last;
break;
}
}
return rightPart;
}
class Iterator{
public:
Iterator() {
now = NULL;
}
Iterator(const std::shared_ptr<Node<T>>& ptr) {
now = ptr;
}
std::shared_ptr<Node<T>> getNode() {
return now;
}
bool operator == (const Iterator& other) const {
return now == other.now;
}
bool operator != (const Iterator& other) const {
return !(*this == other);
}
int operator * () const {
return now->val;
}
Iterator& operator++ () {
now = now->next;
return *this;
}
Iterator operator++ (int) {
auto tmp = *this;
++(*this);
return tmp;
}
private:
std::shared_ptr<Node<T>> now;
};
Iterator begin() const {
return Iterator(first);
}
Iterator end() const {
return Iterator(last);
}
private:
std::shared_ptr<Node<T>> first;
std::shared_ptr<Node<T>> last;
size_t sz;
};
List<int> merge(const List<int>& list1, const List<int>& list2) {
auto it1 = list1.begin(), it2 = list2.begin();
List<int> list;
while (it1 != list1.end() && it2 != list2.end()) {
if (*it1 < *it2) {
list.push_back(*it1);
++it1;
} else {
list.push_back(*it2);
++it2;
}
}
for (; it1 != list1.end(); ++it1) {
list.push_back(*it1);
}
for (; it2 != list2.end(); ++it2) {
list.push_back(*it2);
}
return list;
}
void mergeSort(List<int>& list1) {
if (list1.size() <= 1) return;
int m = list1.size() / 2;
List<int> list2 = list1.splitBy(m);
mergeSort(list1);
mergeSort(list2);
list1 = merge(list1, list2);
}
int main() {
int listSize;
std::cin >> listSize;
List<int> list;
for (int i = 0; i < listSize; ++i) {
int num;
std::cin >> num;
list.push_back(num);
}
mergeSort(list);
for (auto it = list.begin(); it != list.end(); ++it) {
std::cout << (*it) << " ";
}
std::cout << "\n";
return 0;
}