Cod sursa(job #1475745)

Utilizator salam1Florin Salam salam1 Data 24 august 2015 12:17:46
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.05 kb
#include <cstdio>
#include <cstring>
const int NMAX = 500505;
const int FBYTE = (1 << 8) - 1;
const int LMAX = (1 << 8);
int n, a, b, c, A[2][NMAX];
  
int getByte(int idx, int val) {
  return (val >> (idx * 8)) & FBYTE;
}
  
void read() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &A[0][i]);
  }
}
  
void radixSort(int left, int right, int byte) {
  if (byte == 4) {
    return ;
  }
   
  int cnt[LMAX], st[LMAX];
  memset(cnt, 0, sizeof(cnt));
    
  for (int i = left; i <= right; i++) {
    cnt[getByte(byte, A[byte & 1][i])]++;
  }
  st[0] = left - 1;
  for (int i = 1; i <= FBYTE; i++) {
    st[i] = st[i - 1] + cnt[i - 1];
  }
    
  for (int i = left; i <= right; i++) {
    int val = getByte(byte, A[byte & 1][i]);
    A[1 - (byte & 1)][++st[val]] = A[byte & 1][i];
  }
    
  radixSort(left, right, byte + 1);
}
  
int main() {
  freopen("algsort.in", "r", stdin);
  freopen("algsort.out", "w", stdout);
    
  read();
  radixSort(1, n, 0);
    
  for (int i = 1; i <= n; i++)
    printf("%d ", A[0][i]);
  printf("\n");
  return 0;
}