Advertisement
Guest User

NikolaevMisha/5.3/new/new

a guest
Oct 22nd, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.42 KB | None | 0 0
  1. /*
  2.  
  3.  
  4. Дан массив неотрицательных целых 64-разрядных чисел. Количество чисел не больше 1000000. Отсортировать массив методом MSD по битам (бинарный QuickSort).
  5. */
  6.  
  7. #include <iostream>
  8. #include <vector>
  9.  
  10. void Partition(std::vector<long long>& A,int start ,int finish ,int i) // бинарный QuickSort
  11. {
  12.     if(i == -1)
  13.     {
  14.         return;
  15.     }
  16.     int localStart = start;
  17.     int localFinish = finish;
  18.     while(localStart <= localFinish)
  19.     {
  20.         for(;localStart <= finish && (A[localStart] >> i) % 2 == 0 ; ++ localStart) {}
  21.  
  22.         for(;localFinish >= start && (A[localFinish] >> i) % 2 == 1 ; --localFinish) {}
  23.  
  24.         if(localStart < localFinish)
  25.         {
  26.             std::swap(A[localStart] , A[localFinish]);
  27.             ++localStart;
  28.             --localFinish;
  29.         }
  30.     }
  31.     if(localFinish > start)
  32.     {
  33.         Partition(A , start , localFinish , i - 1);
  34.     }
  35.     if(localStart < finish)
  36.     {
  37.         Partition(A , localStart , finish , i - 1);
  38.     }
  39.  
  40.  
  41.  
  42. }
  43.  
  44. int main()
  45. {
  46.     int n;
  47.     std::cin >> n;
  48.     std::vector<long long> A(n);
  49.     for(int i = 0; i < n; ++i)
  50.     {
  51.         std::cin >> A[i];
  52.     }
  53.  
  54.     Partition(A, 0 , n - 1, 63);
  55.     for(int i = 0; i < n; ++i)
  56.     {
  57.         std::cout << A[i] << " ";
  58.     }
  59.     std::cout << '\n';
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement