Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
194 changes: 97 additions & 97 deletions main
Original file line number Diff line number Diff line change
@@ -1,151 +1,151 @@
#include<cstdio>
#include<iostream>
template<typename T> void Swap(T& elem1, T& elem2) {
T other = elem1;
elem1 = elem2;
elem2 = other;
}
#include <iostream>

template <typename T> class Point {
template <typename T> class Node {
public:
T val;
Point *next;
Point(T val) {
Node *next;

Node() {
next = NULL;
}

Node(T val) {
this->val = val;
next = NULL;
}
};

template<typename T> class List {
public:
const size_t size() {
return size_val;
public:
size_t size() const {
return sizeVal;
}

List(size_t sizeVal, Node<T> *first, Node<T> *last) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Почитай про списки инициализации:
http://en.cppreference.com/w/cpp/language/initializer_list

this->sizeVal = sizeVal;
this->first = first;
this->last = last;
}

List() {
size_val = 0;
left = NULL;
right = NULL;
sizeVal = 0;
first = new Node<T>();
last = first;
}

~List() {
Point<T> *cur = left, *cur1;
while (cur != NULL) {
cur1 = cur;
cur = cur->next;
delete(cur1);
if (first == NULL) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nullptr

return;
}
}

void Add(T& val) {
if (right == NULL) {
right = new Point<T>(val);
left = right;
size_val = 1;
} else {
right->next = new Point<T>(val);
right = right->next;
++size_val;
Node<T> *nextHead = first, *head;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Не нужно объявлять в одной строке несколько указателей, можно случайно допустить ошибку вида

Node* a, b;

while (nextHead != last) {
head = nextHead;
nextHead = nextHead->next;
delete head;
}
delete(last);
}

void Add(T val) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const T&

last->val = val;
last->next = new Node<T>();
last = last->next;
++sizeVal;
}

void updFirst(Node<T>* newFirst) {
first = newFirst;
}

Point<T>* begin() const {
return left;
Node<T>* begin() const {
return first;
}

Point<T>* end() const {
return right;
Node<T>* end() const {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Читай комментарий к предыдущей версии. end() не должен возвращать последний элемент.

return last;
}

private:
Point<T> *left, *right;
size_t size_val;
Node<T> *first, *last;
size_t sizeVal;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

size_ лучше, чем sizeVal

};

template <typename T> std::ostream& operator << (std::ostream& out, const List<T> &ans) {
auto cur = ans.begin();
while (cur != ans.end()) {
out << cur->val << ' ';
cur = cur->next;
template <typename T> std::ostream& operator << (std::ostream& out, const List<T> &outVal) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Можно просто val, outVal не добавляет смысла.
template здесь и в других местах лучше перенести на предыдущую строчку.

auto element = outVal.begin();
while (element != outVal.end()) {
out << element->val << ' ';
element = element->next;
}

out << ans.end()->val;
return out;
}

template <typename T> void Sort(Point<T> *first, Point<T> *last) {
if (first == last)
template <typename T> void Sort(List<T>& sortList) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Зачем слово sort в названии параметра?

if (sortList.size() < 2) {
return;
}

if (first->next == last) {
if (first->val > last->val) {
Swap(first->val, last->val);
if (sortList.size() == 2) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Зачем отдельно обрабатывать случай с двойкой?

auto newFirst = sortList.begin()->next;
if (sortList.begin()->val <= newFirst->val) {
return;
}

sortList.begin()->next = sortList.end();
newFirst->next = sortList.begin();
sortList.updFirst(newFirst);
return;
}

size_t length_first = 1, length_second = 1;
Point<T> *cur, *mid1, *mid2, *cur1, *cur2, *fail;
for (cur = first; cur != last; ++length_first) {
cur = cur->next;
}

for (cur = first; length_second != length_first / 2; ++length_second) {
cur = cur->next;
}

mid1 = cur;
mid2 = cur->next;
Sort(first, mid1);
Sort(mid2, last);


cur = first;
cur1 = first->next;
cur2 = mid2;
fail = mid1->next;
while ((cur1 != fail) || (cur2 != last)) {
if ((cur2 == last) || ((cur1 != fail) && (cur1->val <= cur2->val))) {
cur->next = cur1;
cur1 = cur1->next;
cur = cur->next;
} else {
cur->next = cur2;
cur2 = cur2->next;
cur = cur->next;
}
auto midNode = sortList.begin();
for (size_t i = 0; i < sortList.size() / 2; ++i) {
midNode = midNode->next;
}

cur->next = last;
cur = first;
while ((cur->next != last) && (cur->next->val < cur->val)) {
Swap(cur->next->val, cur->val);
cur = cur->next;
auto firstList = List<T>(sortList.size() / 2, sortList.begin(), midNode);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Сюда и далее бы переводов строк навставлять между логическими блоками, а то читается очень тяжело.

auto lastList = List<T>(sortList.size() - sortList.size() / 2, midNode, sortList.end());
Sort(firstList);
Sort(lastList);
auto sortSize = sortList.size();
auto headFirst = firstList.begin();
auto headLast = lastList.begin();
Node<T>* head;
if (headFirst->val <= headLast->val) {
head = headFirst;
headFirst = headFirst->next;
} else {
head = headLast;
headLast = headLast->next;
}

T max_val = last->val;
for (cur = first; cur != last; cur = cur->next) {
if (cur->val > max_val) {
Swap(cur->val, max_val);
auto tail = head;
while ((headFirst != firstList.end()) || (headLast != lastList.end())) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Скобки вокруг выражений лишние.

if (headLast == lastList.end() || (headFirst != firstList.end() && headFirst->val <= headLast->val)) {
tail->next = headFirst;
headFirst = headFirst->next;
} else {
tail->next = headLast;
headLast = headLast->next;
}
tail = tail->next;
}

last->val = max_val;
}
tail->next = sortList.end();
firstList.updFirst(NULL);
lastList.updFirst(NULL);
sortList.updFirst(head);
}


int main() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int n, val;
List<int> input_file;
List<int> inputList;
std::cin >> n;
for (int i = 0; i < n; ++i) {
std::cin >> val;
input_file.Add(val);
inputList.Add(val);
}

Sort(input_file.begin(), input_file.end());
Sort(inputList);

std::cout << input_file << "\n";
std::cout << inputList << '\n';
}
Loading