Advertisement
dimon-torchila

Untitled

Jan 2nd, 2023
863
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.80 KB | None | 0 0
  1. #include "lab.h"
  2.  
  3. using namespace std;
  4.  
  5. void countingSort(lab::Queue<int>& q){
  6.     int mx = INT_MIN;
  7.     int size = q.size();
  8.     for(int i = 0; i < q.size(); ++i){
  9.         int front = q.front();
  10.         mx = max(mx, front);
  11.         q.pop();
  12.         q.push(front);
  13.     }
  14.     auto count = vector<int>(mx + 1, 0);
  15.     for(int i = 0; i < size; ++i){
  16.         int front = q.front();
  17.         count[front]++;
  18.         q.pop();
  19.     }
  20.     for(int i = 0; i < count.size(); ++i){
  21.         for(int j = 0; j < count[i]; ++j){
  22.             q.push(i);
  23.         }
  24.     }
  25. }
  26.  
  27. int main() {
  28.     srand(time(nullptr));
  29.     lab::Queue<int> queue(7);
  30.     for(int i = 0; i < queue.size(); ++i)
  31.         queue.push(rand() % 10);
  32.     queue.print();
  33.     cout << endl;
  34.     countingSort(queue);
  35.     queue.print();
  36. }
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement