Pagini recente » Clasamentul arhivei Infoarena Monthly | Istoria paginii runda/delaceorashimulare | tema | Clasamentul arhivei de probleme | Cod sursa (job #995413)
Cod sursa(job #995413)
#include <fstream>
#include <vector>
using namespace std;
class Sortable {
private:
vector<unsigned int> numbers_;
const unsigned int size_;
unsigned int part(unsigned int left, unsigned int right) {
unsigned int pivot = numbers_[(left + right) / 2];
unsigned int left_index = left - 1;
unsigned int right_index = right + 1;
unsigned int aux;
while (1) {
do {left_index++;} while (numbers_[left_index] < pivot);
do {right_index--;} while (numbers_[right_index] > pivot);
if (left_index < right_index)
{
aux = numbers_[left_index];
numbers_[left_index] = numbers_[right_index];
numbers_[right_index] = aux;
} else {
return right_index;
}
}
}
void recursion(unsigned int left, unsigned int right) {
unsigned int pivot;
if (left < right) {
pivot = part(left, right);
recursion(left, pivot);
recursion(pivot + 1, right);
}
}
public:
Sortable(unsigned int size, const vector<unsigned int>& numbers) : size_(size) {
numbers_ = numbers;
}
vector<unsigned int> numbers() {
return numbers_;
}
void sort() {
recursion(0, size_ - 1);
}
};
ifstream in("algsort.in");
ofstream out("algsort.out");
int main() {
unsigned int size;
unsigned int element;
vector<unsigned int> numbers;
in>>size;
for (unsigned int i = 0; i < size; i++) {
in >> element;
numbers.push_back(element);
}
Sortable sortable(size, numbers);
sortable.sort();
numbers = sortable.numbers();
for (unsigned int i = 0; i < size; i++) {
out << numbers[i] << " ";
}
return 0;
}