#include <algorithm>
#include <forward_list>
int mapping(int x, int base) {
return base ? (base > 0 ? x - base : x + -base) : x;
}
typename std::forward_list<int>::const_iterator before_last(const std::forward_list<int> &list) {
auto begin {list.cbefore_begin()};
while(true) {
auto after_begin {begin};
++after_begin;
if(after_begin == list.cend()) {
return begin;
}
++begin;
}
}
void bucket_sort(int arr[], int size) {
const auto max {*std::max_element(arr, arr + size)};
const auto min {*std::min_element(arr, arr + size)};
const auto range {max - min + 1};
auto list_array {new std::forward_list<int>[range]};
for(auto i {0}; i < size; ++i) {
auto index {mapping(arr[i], min)};
list_array[index].insert_after(before_last(list_array[index]), arr[i]);
}
std::forward_list<int> r {};
for(auto i {0}; i < range; ++i) {
r.merge(list_array[i]);
}
delete[] list_array;
auto it {r.cbegin()};
for(auto i {0}; it not_eq r.cend(); ++i) {
arr[i] = *it++;
}
}