Advertisement
Tark_Wight

Recurrent mergeSort for a List

Aug 27th, 2023 (edited)
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.53 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <memory>
  3.  
  4.  
  5. class DoubleNode {
  6. public:
  7.     DoubleNode(int data) : data(data) {}
  8.     int data;
  9.     std::shared_ptr<DoubleNode> previous = nullptr;
  10.     std::shared_ptr<DoubleNode> next = nullptr;
  11.     int index = 0;
  12. };
  13.  
  14.  
  15. class DoublyLinkedListOfIntegers {
  16. public:
  17.     std::shared_ptr<DoubleNode> pFirstNode = nullptr;
  18.     std::shared_ptr<DoubleNode> pLastNode = nullptr;
  19.     int count = 0;
  20.    
  21.     void add(int data) {
  22.         std::shared_ptr<DoubleNode> newNode{ std::make_shared<DoubleNode>(data) };
  23.        
  24.         if (!pFirstNode) {
  25.             pFirstNode = newNode;
  26.         } else {
  27.             pLastNode->next = newNode;
  28.             newNode->previous = pLastNode;
  29.         }
  30.        
  31.         newNode->index = count;
  32.         pLastNode = newNode;
  33.         ++count;
  34.     }
  35.    
  36.     static void mergeSort(DoublyLinkedListOfIntegers& linkedList) {
  37.         int amount = linkedList.count;
  38.         int left, middle, right;
  39.        
  40.         for (int step = 1; step < amount; step *= 2) {
  41.             for (int start = 0; start < amount; start += step * 2) {
  42.                 left = start;
  43.                 middle = std::min(start + step - 1, amount - 1);
  44.                 right = std::min(start + 2 * step - 1, amount - 1);
  45.                 merge(linkedList, left, middle, right);
  46.             }
  47.         }
  48.     }
  49.    
  50.     static void merge(DoublyLinkedListOfIntegers& linkedList, int left, int middle, int right) {
  51.         int leftAmount = middle - left + 1;
  52.         int rightAmount = right - middle;
  53.        
  54.         DoublyLinkedListOfIntegers leftList;
  55.         DoublyLinkedListOfIntegers rightList;
  56.        
  57.         std::shared_ptr<DoubleNode> listNode{ linkedList.pFirstNode };
  58.         for (int i = 0; i < left; ++i) {
  59.             listNode = listNode->next;
  60.         }
  61.         std::shared_ptr<DoubleNode> leftListNode{ listNode };
  62.        
  63.         for (int i = 0; i < leftAmount; ++i) {
  64.             leftList.add(listNode->data);
  65.             listNode = listNode->next;
  66.         }
  67.        
  68.         for (int j = 0; j < rightAmount; ++j) {
  69.             rightList.add(listNode->data);
  70.             listNode = listNode->next;
  71.         }
  72.        
  73.         int indexForLeftAmount = 0;
  74.         int indexForRightAmount = 0;
  75.        
  76.         listNode = leftListNode;
  77.         leftListNode = leftList.pFirstNode;
  78.         std::shared_ptr<DoubleNode> rightListNode = rightList.pFirstNode;
  79.         int k = left;
  80.        
  81.         while (indexForLeftAmount < leftAmount && indexForRightAmount < rightAmount) {
  82.             if (leftListNode->data <= rightListNode->data) {
  83.                 listNode->data = leftListNode->data;
  84.                 leftListNode = leftListNode->next;
  85.                 ++indexForLeftAmount;
  86.                 listNode = listNode->next;
  87.             } else {
  88.                 listNode->data = rightListNode->data;
  89.                 rightListNode = rightListNode->next;
  90.                 ++indexForRightAmount;
  91.                 listNode = listNode->next;
  92.             }
  93.             ++k;
  94.         }
  95.        
  96.         while (indexForLeftAmount < leftAmount) {
  97.             listNode->data = leftListNode->data;
  98.             leftListNode = leftListNode->next;
  99.             listNode = listNode->next;
  100.             ++indexForLeftAmount;
  101.             ++k;
  102.         }
  103.        
  104.         while (indexForRightAmount < rightAmount) {
  105.             listNode->data = rightListNode->data;
  106.             rightListNode = rightListNode->next;
  107.             listNode = listNode->next;
  108.             ++indexForRightAmount;
  109.             ++k;
  110.         }
  111.     }
  112.    
  113.     class Iterator {
  114.     public:
  115.         Iterator(std::shared_ptr<DoubleNode> node) : current(node) {}
  116.         Iterator& operator++() {
  117.             current = current->next;
  118.             return *this;
  119.         }
  120.        
  121.         bool operator != (const Iterator& other) const {
  122.             return current != other.current;
  123.         }
  124.        
  125.         int operator*() const {
  126.             return current->data;
  127.         }
  128.        
  129.     private:
  130.         std::shared_ptr<DoubleNode> current;
  131.     };
  132.    
  133.     Iterator begin() {
  134.         return Iterator(pFirstNode);
  135.     }
  136.    
  137.     Iterator end() {
  138.         return Iterator(nullptr);
  139.     }
  140. };
  141.  
  142.  
  143. int main(int argc, char* argv[]) {
  144.     int value;
  145.     DoublyLinkedListOfIntegers linkedList;
  146.    
  147.     while (std::cin >> value) {
  148.         linkedList.add(value);
  149.     }
  150.    
  151.     DoublyLinkedListOfIntegers::mergeSort(linkedList);
  152.    
  153.     for (int node : linkedList) {
  154.         std::cout << node << " ";
  155.     }
  156.    
  157.     return 0;
  158. }
  159.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement