Pagini recente » Cod sursa (job #1221053) | Cod sursa (job #2844179) | Cod sursa (job #1373257) | Cod sursa (job #1268137) | Cod sursa (job #1656224)
#include <iostream>
#include <fstream>
#include <queue>
#include <stdio.h>
#include <algorithm>
#define MAXBUF 10000
#define RAD 0xFF
#define RADS 8
#define get_byte(x) ((x>>(byte * 8))&RAD)
using namespace std;
ifstream in("algsort.in", ifstream::binary);
ofstream out("algsort.out", ofstream::binary);
int mx;
int arr[500003];
int n;
char c[MAXBUF] = {'\0'};
char o[MAXBUF];
int k = MAXBUF;
int q = 0;
const int SZ = 1<<RADS;
queue<int> qu[SZ];
queue<int> emp;
inline char nextch() {
if(k == MAXBUF) {
in.read(c, MAXBUF);
k = 0;
}
return c[k++];
}
inline int read() {
int x = 0;
char ch = nextch();
while(!isdigit(ch))
ch = nextch();
while(isdigit(ch)) {
x = x*10 + ch - '0';
ch = nextch();
}
return x;
}
inline void putch(char ch) {
o[q++] = ch;
if(q == MAXBUF) {
out.write(o, MAXBUF);
q = 0;
}
}
int s;
char c2[10];
inline void put(int x) {
s = 0;
do {
c2[s++] = x%10;
x /= 10;
} while(x > 0);
while(s > 0) {
s--;
putch(c2[s]+'0');
}
}
void countSort(int byte) {
for(int i = 0; i < SZ; i++)
qu[i] = emp;
int mi = SZ;
int mx = 0;
int t;
for(int i = 0; i < n; i++) {
t = get_byte(arr[i]);
qu[t].push(arr[i]);
if(t < mi)
mi = t;
if(t > mx)
mx = t;
}
int cnt = 0;
for(int i = mi; i <= mx; i++) {
while(!qu[i].empty()) {
arr[cnt++] = qu[i].front();
qu[i].pop();
}
}
}
void radixsort() {
for(int byte = 0; byte < 4; byte++)
countSort(byte);
}
int main() {
n = read();
for(int i = 0; i < n; i++) {
arr[i] = read();
if(arr[i] > mx)
mx = arr[i];
}
radixsort();
for (int i = 0; i < n; i++) {
put(arr[i]);
putch(' ');
}
out.write(o, q);
return 0;
}