Files
ARPZ-s1-pr5/task5.cpp
2024-12-14 12:19:04 +03:00

30 lines
1.2 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include <iostream>
void shellSort(int arr[], int n)
{
// Сортировка Шелла
for (int gap = n/2; gap > 0; gap /= 2)
{
// Выполнение промежуточной сортировки вставками.
for (int i = gap; i < n; i += 1)
{
// Сохраняем элемент arr[i] во временной переменной temp,
// чтобы на этой позиции можно было оставить "отверстие".
int temp = arr[i];
// Передвигаем ранее отсортированные элементы выше,
// пока не будет найдена правильная позиция для a[i]
int j; // Объявляем j здесь, т.к. позже еще будет нужна
for (j = i; j >= gap && arr[j - gap] > temp; j -= gap)
{
// Сдвигаем ранее отсортированные элементы.
// "Отверстие" позволяет это сделать.
arr[j] = arr[j - gap];
}
// Помещаем сохраненный элемент в его правильную позицию
arr[j] = temp;
}
}
}