Pagini recente » Cod sursa (job #1958949) | Cod sursa (job #2267137) | Cod sursa (job #1935870) | Cod sursa (job #337963) | Cod sursa (job #2726419)
#include <fstream>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int fragments = 256;
fstream fin("algsort.in");
ofstream fout("algsort.out");
vector<int> initialNumbers;
double EPS = 1e-6;
void akySort(vector<int>& numbers){
if (numbers.size() < fragments*4){
sort(numbers.begin(), numbers.end());
return;
}
int maxValue = numbers[0];
for (auto const &el : numbers){
maxValue = max(maxValue, el);
}
double step = (double)(maxValue) / (double)(fragments);
vector<int> buckets[fragments];
for (auto const &el : numbers){
int index = min((int)(el / step), 255);
buckets[index].push_back(el);
}
vector<int> result;
for (auto & bucket : buckets){
if (bucket.size() == numbers.size()){
sort(bucket.begin(), bucket.end());
goto push_elem;
}
akySort(bucket);
push_elem:
for (auto & el : bucket){
result.push_back(el);
}
}
numbers = result;
}
int main() {
int n = 1e5;
fin >> n;
initialNumbers.resize(n);
for (auto &el : initialNumbers){
fin >> el;
}
akySort(initialNumbers);
for (auto &el : initialNumbers){
fout << el << " ";
}
return 0;
}