Cod sursa(job #2642698)

Utilizator lucametehauDart Monkey lucametehau Data 16 august 2020 19:43:59
Problema Sortare prin comparare Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.76 kb

#include <fstream>
#include <vector>

using namespace std;

ifstream cin ("algsort.in");
ofstream cout ("algsort.out");

int n;

int v[500005], tmp[500005], cnt[16], nr[16];

int main() {
  cin >> n;
  for(int i = 1; i <= n; i++)
    cin >> v[i];
  for(int j = 0; j < 8; j++) {
    for(int i = 1; i <= n; i++)
      cnt[(v[i] >> (j * 4)) & 15]++;
    for(int i = 1; i < 16; i++)
      cnt[i] += cnt[i - 1];
    for(int i = 1; i <= n; i++) {
      int k = ((v[i] >> (j * 4)) & 15);
      nr[k]++;
      tmp[(k > 0 ? cnt[k - 1] : 0) + nr[k]] = v[i];
    }
    for(int i = 1; i <= n; i++)
      v[i] = tmp[i];
    for(int i = 0; i < 16; i++)
      cnt[i] = nr[i] = 0;
  }
  for(int i = 1; i <= n; i++)
    cout << v[i] << " ";
  return 0;
}