Advertisement
Neveles

© 2020 Neveles. All rights reserved.

Apr 15th, 2020
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.13 KB | None | 0 0
  1. void ShellSort(int n, int *arr, int &counter)
  2. {
  3.   counter = 0;
  4.   for (int step = n / 2; step > 0; step /= 2)
  5.   {
  6.     for (int i = 0; i < step; ++i)
  7.     {
  8.       List<int> list;
  9.       for (int curr = i; curr < n; curr += step)
  10.       {
  11.         counter += (list += arr[curr]);
  12.       }
  13.       List<int>::Elem* currElem = list.head_;
  14.       for (int curr = i; curr < n; curr += step)
  15.       {
  16.         arr[curr] = currElem->getValue();
  17.         currElem = currElem->getNext();
  18.       }
  19.     }
  20.   }
  21. }
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29. #ifndef LIST_H
  30. #define LIST_H
  31.  
  32. #include <iostream>
  33.  
  34. template<class T>
  35. class List {
  36. public:
  37.  
  38.   List() : tail_(nullptr), head_(nullptr) {}
  39.  
  40.   int operator+=(T value)
  41.   {
  42.     int counter = 0;
  43.     if (head_ == nullptr)
  44.     {
  45.       head_ = new Elem(value, nullptr);
  46.       tail_ = head_;
  47.       return 1;
  48.     }
  49.     Elem* curr = head_;
  50.     if (head_->getValue() > value)
  51.     {
  52.       head_ = new Elem(value, curr);
  53.       return 1;
  54.     }
  55.     else if (tail_->getValue() < value)
  56.     {
  57.       tail_->setNext(new Elem(value, nullptr));
  58.       tail_ = tail_->getNext();
  59.       return 1;
  60.     }
  61.     else
  62.     {
  63.       while (curr != nullptr)
  64.       {
  65.         counter++;
  66.         if (curr->getValue() >= value)
  67.         {
  68.           T tmpValue = curr->getValue();
  69.           Elem* tmpPointer = curr->getNext();
  70.           curr->setValue(value);
  71.           curr->setNext(new Elem(tmpValue, tmpPointer));
  72.           if (curr == tail_)
  73.           {
  74.             tail_ = curr->getNext();
  75.           }
  76.           return counter;
  77.         }
  78.         curr = curr->getNext();
  79.       }
  80.     }
  81.   }
  82.  
  83.  
  84.   ~List()
  85.   {
  86.     while (head_ != nullptr)
  87.     {
  88.       Elem* curr = head_;
  89.       head_ = head_->getNext();
  90.       delete (curr);
  91.     }
  92.   }
  93.  
  94.  
  95.   bool isEmpty() const
  96.   {
  97.     return head_ == nullptr;
  98.   }
  99.  
  100.   struct Elem
  101.   {
  102.   public:
  103.     Elem(T value, Elem* next) : value_(value), next_(next) {}
  104.  
  105.     Elem* getNext() const
  106.     {
  107.       return next_;
  108.     }
  109.  
  110.     void setValue(T value)
  111.     {
  112.       value_ = value;
  113.     }
  114.  
  115.     void setNext(Elem* next)
  116.     {
  117.       next_ = next;
  118.     }
  119.  
  120.     T getValue() const
  121.     {
  122.       return value_;
  123.     }
  124.  
  125.   private:
  126.     Elem* next_;
  127.     T value_;
  128.   };
  129.  
  130.   Elem* head_;
  131.   Elem* tail_;
  132. };
  133.  
  134. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement