Advertisement
CyberN00b

govnosort

Dec 10th, 2022
995
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.14 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. template<typename T, typename Compare>
  6. void my_sort(T t_begin, T t_end, Compare comp) {
  7.     for (T it = t_begin + 1; it != t_end; ++it) {
  8.         if (comp(*it, *(it - 1))) {
  9.             T left = t_begin, right = it;
  10.             if (comp(*left, *it)) {
  11.                 T mid = left + distance(left, right) / 2;
  12.                 while (mid != left) {
  13.                     if (comp(*mid, *it)) {
  14.                         left = mid;
  15.                     } else {
  16.                         right = mid;
  17.                     }
  18.                     mid = left + distance(left, right) / 2;
  19.                 }
  20.             } else
  21.                 right = left;
  22.             for (T t = it; t != right; --t) {
  23.                 swap(*t, *(t - 1));
  24.             }
  25.         }
  26.     }
  27. }
  28.  
  29. template<typename T>
  30. void my_sort(T t_begin, T t_end) {
  31.     my_sort(t_begin, t_end, [](auto& it1, auto& it2){return it1 < it2;});
  32. }
  33.  
  34. int main() {
  35.     int a;
  36.     cin >> a;
  37.     vector<int> vc(a);
  38.     for (auto& x : vc)
  39.         cin >> x;
  40.     my_sort(vc.begin(), vc.end());
  41.     for (auto& x : vc)
  42.         cout << x << ' ';
  43.  
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement