vadimk772336

Untitled

Apr 10th, 2022
1,028
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.92 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4. #include <array>
  5. using namespace std;
  6.  
  7.  
  8. struct suffix
  9. {
  10.     int index; // To store original index
  11.     int rank[2]; // To store ranks and next rank pair
  12. };
  13.  
  14. bool operator<(const struct suffix& a, const struct suffix& b)
  15. {
  16.     if (a.rank[0] == b.rank[0])
  17.     {
  18.         if (a.rank[1] < b.rank[1])
  19.             return true;
  20.         else
  21.             return false;
  22.     }
  23.     else if (a.rank[0] < b.rank[0])
  24.         return true;
  25.     else
  26.         return false;
  27.  
  28. }
  29.  
  30. suffix* radix_sort(suffix A[], int n)
  31. {
  32.     int C[n];
  33.     vector<suffix> B(n);
  34.  
  35.     int d, tmp, count;
  36.  
  37.     // Сортирую по столбцам рангов, сначала 0 потом 1
  38.     for (int i = 0; i < 2; ++i)
  39.     {
  40.         for (int j = 0; j < n; ++j)
  41.         {
  42.             C[j] = 0;
  43.         }
  44.        
  45.         for (int j = 0; j < n; ++j)
  46.         {
  47.             d = A[j].rank[i];
  48.             C[d]++;
  49.         }
  50.  
  51.         count = 0;
  52.         for (int j = 0; j < n; ++j)
  53.         {
  54.             tmp = C[j];
  55.             C[j] = count;
  56.             count += tmp;
  57.         }
  58.        
  59.         for (int j = 0; j < n; ++j)
  60.         {
  61.             d = A[j].rank[i];
  62.             B[C[d]] = A[j];
  63.             C[d]++;
  64.         }
  65.  
  66.         for (int j = 0; j < n; ++j)
  67.         {
  68.             A[i] = B[i];
  69.         }
  70.     }
  71.    
  72.     cout << "12312321312";
  73.     for (int i=0 ; i < 3; ++i)
  74.     {
  75.         cout << A[i].rank[0] << " " << A[i].rank[1] << endl;
  76.     }
  77.    
  78.     return A;
  79. }
  80.  
  81.  
  82. int main()
  83. {
  84.    
  85.     //13 31 22
  86.     suffix arr[3];
  87.    
  88.     arr[0].rank[0] = 1;
  89.     arr[0].rank[1] = 3;
  90.     arr[1].rank[0] = 3;
  91.     arr[1].rank[1] = 1;
  92.     arr[2].rank[0] = 2;
  93.     arr[2].rank[1] = 2;
  94.    
  95.     suffix* B = radix_sort(arr, 3);
  96.    
  97.    
  98.     for (int i=0 ; i < 3; ++i)
  99.     {
  100.         cout << B[i].rank[0] << " " << B[i].rank[1] << endl;
  101.     }
  102.    
  103.    
  104.     return 0;
  105. }
  106.  
Advertisement
Add Comment
Please, Sign In to add comment