Например, у меня есть этот std::vector
Код: Выделить всё
11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
( соответствующие значения: 10, 7, 4, 1). Результирующий массив будет выглядеть следующим образом:
Код: Выделить всё
11, 1, 9, 8, 4, 6, 5, 7, 3, 2, 10, 0
Код: Выделить всё
#include
#include
#include
void sort_constant_jump(std::vector &foo, int start, int jump)
{
std::vector need_to_sort;
for(int index = start; index < foo.size(); index += jump)
{
need_to_sort.push_back(foo[index]);
}
std::sort(need_to_sort.begin(), need_to_sort.end());
for(int index = 0; index < need_to_sort.size(); ++index)
{
foo[index * jump + start] = need_to_sort[index];
}
}
int main()
{
std::vector foo {11, 1, 9, 8, 4, 6, 5, 7, 3, 2, 10, 0};
sort_constant_jump(foo, 1, 3);
for(int number : foo)
std::cout
Подробнее здесь: [url]https://stackoverflow.com/questions/79239044/how-to-sort-elements-whose-distance-is-constant[/url]