Cod sursa(job #2901024)

Utilizator petru.theodor07Petru Cristea petru.theodor07 Data 12 mai 2022 19:29:54
Problema Radix Sort Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.13 kb
#include <bits/stdc++.h>
using namespace std;

ifstream fin("radixsort.in");
ofstream fout("radixsort.out");

int getMax(int v[], int n) {
  int max = v[0];
  for (int i = 1; i < n; i++)
    if (v[i] > max)
      max = v[i];
  return max;
}

void countingSort(int v[], int size, int place) {
  const int max = 10;
  int output[size];
  int count[max];

  for (int i = 0; i < max; ++i)
    count[i] = 0;

  for (int i = 0; i < size; i++)
    count[(v[i] / place) % 10]++;

  for (int i = 1; i < max; i++)
    count[i] += count[i - 1];

  for (int i = size - 1; i >= 0; i--) {
    output[count[(v[i] / place) % 10] - 1] = v[i];
    count[(v[i] / place) % 10]--;
  }

  for (int i = 0; i < size; i++)
    v[i] = output[i];
}

void radixsort(int v[], int size) {
  int max = getMax(v, size);

  for (int place = 1; max / place > 0; place *= 10)
    countingSort(v, size, place);
}

int main() {
  int n, a, b, c;
  fin>>n>>a>>b>>c;
  int v[n];
  v[0] = b % c;
    for(int i = 1; i < n; i++)
        v[i] = (1LL * a * v[i-1] % c + b) % c;
  radixsort(v, n);
  for(int i=0; i<n; i+=10)
    fout<<v[i]<<' ';
}