Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.64 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. class node {
  4. public:
  5. int elem;
  6. node* next;
  7. };
  8.  
  9. class nodet {
  10. public:
  11. int elem;
  12. nodet* next;
  13. nodet* prev;
  14. };
  15.  
  16. node* push_before(node* list, int elem) {
  17. node* p = new node;
  18. p->elem = elem;
  19. p->next = list;
  20. return p;
  21. }
  22.  
  23. void push(nodet*& list, int elem, nodet*& last) {
  24. if (list == NULL) {
  25. list = new nodet;
  26. list->next = NULL;
  27. list->prev = NULL;
  28. list->elem = elem;
  29. last = list;
  30. return;
  31. }
  32. nodet* p = new nodet;
  33. p->elem = elem;
  34. p->prev = last;
  35. p->next = NULL;
  36. last->next = p;
  37. last = p;
  38. }
  39.  
  40. void merge(nodet*& listf, nodet*& lastf, nodet* lists, nodet* lasts) {
  41. nodet* listp = listf;
  42. listf = NULL;
  43. nodet* lastp = lastf;
  44. lastf = NULL;
  45. while (listp != NULL && lists != NULL) {
  46. if (listp->elem > lists->elem) {
  47. if (listf == NULL) {
  48. listf = lists;
  49. lists->prev = NULL;
  50. nodet* tmp = lists->next;
  51. lists->next = NULL;
  52. lists = tmp;
  53. lastf = listf;
  54. } else {
  55. lists->prev = lastf;
  56. lastf->next = lists;
  57. nodet* tmp = lists->next;
  58. lists->next = NULL;
  59. lists = tmp;
  60. lastf = lastf->next;
  61. }
  62. } else {
  63. if (listf == NULL) {
  64. listf = listp;
  65. listp->prev = NULL;
  66. nodet* tmp = listp->next;
  67. listp->next = NULL;
  68. listp = tmp;
  69. lastf = listf;
  70. } else {
  71. listp->prev = lastf;
  72. lastf->next = listp;
  73. nodet* tmp = listp->next;
  74. listp->next = NULL;
  75. listp = tmp;
  76. lastf = lastf->next;
  77. }
  78. }
  79. }
  80. while (listp != NULL) {
  81. listp->prev = lastf;
  82. lastf->next = listp;
  83. lastf = listp;
  84. nodet* tmp = listp->next;
  85. listp->next = NULL;
  86. listp = tmp;
  87. }
  88. while (lists != NULL) {
  89. lists->prev = lastf;
  90. lastf->next = lists;
  91. lastf = lists;
  92. nodet* tmp = lists->next;
  93. lists->next = NULL;
  94. lists = tmp;
  95.  
  96. }
  97. }
  98.  
  99. void mergesort(nodet*& listf, nodet*& lastf) {
  100. if (listf != lastf && listf != NULL&& lastf != NULL) {
  101. nodet* listp = listf;
  102. nodet* lastp = lastf;
  103. while (listp != lastp) {
  104. if (listp->next != lastp) {
  105. lastp = lastp->prev;
  106. listp = listp->next;
  107. } else {
  108. break;
  109. }
  110. }
  111. nodet* list1 = listf;
  112. nodet* last1 = listp;
  113. nodet* list2 = listp->next;
  114. listp->next->prev = NULL;
  115. listp->next = NULL;
  116. nodet* last2 = lastf;
  117. mergesort(list1, last1);
  118. mergesort(list2, last2);
  119. merge(list1, last1, list2, last2);
  120. listf = list1;
  121. lastf = last1;
  122. }
  123. }
  124.  
  125. node* sorth(node* list, int size) {
  126. if (size > 1) {
  127. node* miner = NULL;
  128. node* bigger = NULL;
  129. node* startm = NULL;
  130. node* startb = NULL;
  131. node* starte = NULL;
  132. int sizeb = 0, sizem = 0, sizee = 0;
  133. node* eq = NULL;
  134. int piv = list->elem;
  135. while (list != NULL) {
  136. if (list->elem < piv) {
  137. if (miner != NULL) {
  138. miner->next = list;
  139. miner = miner->next;
  140. } else {
  141. miner = list;
  142. startm = list;
  143. }
  144. ++sizem;
  145. }
  146.  
  147. if (list->elem > piv) {
  148. if (bigger != NULL) {
  149. bigger->next = list;
  150. bigger = bigger->next;
  151. } else {
  152. bigger = list;
  153. startb = list;
  154. }
  155. ++sizeb;
  156. }
  157.  
  158. if (list->elem == piv) {
  159. if (eq != NULL) {
  160. eq->next = list;
  161. eq = eq->next;
  162. } else {
  163. eq = list;
  164. starte = list;
  165. }
  166. ++sizee;
  167. }
  168.  
  169. node* a = list->next;
  170. list->next = NULL;
  171. list = a;
  172. }
  173. startm = sorth(startm, sizem);
  174. startb = sorth(startb, sizeb);
  175. int slu = 0;
  176. node* start = startm;
  177. while (startm != NULL && startm->next != NULL) {
  178. startm = startm->next;
  179. }
  180. if (startm != NULL) {
  181. startm->next = starte;
  182. } else {
  183. slu = 1;
  184. start = starte;
  185. }
  186. while (starte != NULL && starte->next != NULL) {
  187. starte = starte->next;
  188. }
  189. if (starte != NULL) {
  190. starte->next = startb;
  191. } else {
  192. if (slu == 1) {
  193. return startb;
  194. } else {
  195. startm->next = startb;
  196. return start;
  197. }
  198. }
  199. return start;
  200. }
  201. return list;
  202. }
  203.  
  204. int main() {
  205. nodet* list = NULL;
  206. nodet* last = NULL;
  207. int num;
  208. std::cin >> num;
  209. for (int i = 0; i < num; ++i) {
  210. int count;
  211. std::cin >> count;
  212. push(list, count, last);
  213. }
  214. mergesort(list, last);
  215. for (int i = 0; i < num; ++i) {
  216. std::cout << list->elem << ' ';
  217. list = list->next;
  218. }
  219. return 0;
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement