Advertisement
Neveles

© 2020 Neveles. All rights reserved.

Apr 15th, 2020
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.83 KB | None | 0 0
  1. void HibbardSort(int n, int* arr, int& counter)
  2. {
  3.   counter = 0;
  4.   int k = (int)log2(n + 1);
  5.   for (int step = pow(2, k) - 1; step > 0; step = pow(2, k) - 1)
  6.   {
  7.     for (int i = 0; i < step; ++i)
  8.     {
  9.       List<int> list;
  10.       for (int curr = i; curr < n; curr += step)
  11.       {
  12.         counter += (list += arr[curr]);
  13.       }
  14.       List<int>::Elem* currElem = list.head_;
  15.       for (int curr = i; curr < n; curr += step)
  16.       {
  17.         arr[curr] = currElem->getValue();
  18.         currElem = currElem->getNext();
  19.       }
  20.     }
  21.     k--;
  22.   }
  23. }
  24.  
  25. void SedgewickSort(int n, int* arr, int& counter)
  26. {
  27.   counter = 0;
  28.   int k = 2 * (int)log2((n - 1) / 9);
  29.   while (k >= 0)
  30.   {
  31.     int step;
  32.     if (k % 2 == 0)
  33.     {
  34.       step = 9 * pow(2, k) - 9 * pow(2, k / 2) + 1;
  35.     }
  36.     else
  37.     {
  38.       step = 8 * pow(2, k) - 6 * pow(2, (k + 1) / 2) + 1;
  39.     }
  40.     for (int i = 0; i < step; ++i)
  41.     {
  42.       List<int> list;
  43.       for (int curr = i; curr < n; curr += step)
  44.       {
  45.         counter += (list += arr[curr]);
  46.       }
  47.       List<int>::Elem* currElem = list.head_;
  48.       for (int curr = i; curr < n; curr += step)
  49.       {
  50.         arr[curr] = currElem->getValue();
  51.         currElem = currElem->getNext();
  52.       }
  53.     }
  54.     k--;
  55.   }
  56. }
  57.  
  58. int binSearch(int* arr, int value, int begin, int end)
  59. {
  60.   if (begin == end)
  61.   {
  62.     if (value > arr[begin])
  63.     {
  64.       return begin + 1;
  65.     }
  66.     return begin;
  67.   }
  68.  
  69.   int mid = (begin + end) / 2;
  70.   if (arr[mid] > value)
  71.   {
  72.     return binSearch(arr, value, begin, mid);
  73.   }
  74.   else
  75.   {
  76.     return binSearch(arr, value, mid + 1, end);
  77.   }
  78.  
  79. }
  80.  
  81. void binaryInsertSort(int n, int* arr, int& counter)
  82. {
  83.   counter = 0;
  84.   for (int i = 1; i < n; ++i)
  85.   {
  86.     int position = binSearch(arr, arr[i], 0, i - 1);
  87.     for (int j = i - 1; j >= position; --j)
  88.     {
  89.       ++counter;
  90.       std::swap(arr[j], arr[j + 1]);
  91.     }
  92.     ++counter;
  93.   }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement