Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- vector<int> a;
- void print() {
- cout << "state: ";
- for(auto x: a) {
- cout << x << " ";
- }
- cout << endl;
- }
- void count_sort(int n, int pos) {
- vector<int> b(n), count(10, 0);
- for (int i = 0; i < n; i++)
- count[(a[i] / pos) % 10]++;
- for (int i = 1; i < 10; i++)
- count[i] += count[i - 1];
- for (int i = n - 1; i >= 0; i--) {
- b[count[(a[i] / pos) % 10] - 1] = a[i];
- count[(a[i] / pos) % 10]--;
- }
- for (int i = 0; i < n; i++)
- a[i] = b[i];
- }
- void radix_sort(vector<int> a, int n) {
- int k = 0;
- for (auto x: a) k = max(k, x);
- for (int pos = 1; k / pos > 0; pos *= 10){
- cout<<"pos "<<pos<<"s"<<endl;
- count_sort(n, pos);
- print();
- }
- }
- int main() {
- int n;
- //cout << "enter size & elements of the array \n";
- cin >> n;
- a.resize(n);
- for (auto &x: a) cin >> x;
- radix_sort(a, n);
- cout << "\nradix sorted array ";
- for (auto x: a) cout<<x<<" ";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment