Shell Sort
#include <iostream>
using namespace std;
void shellSort(int *arr, int n)
{
int gap = 1;
while (gap < n / 3)
{
gap = 3 * gap + 1;
}
while (gap >= 1)
{
for (int i = gap; i < n; i++)
{
for (int j = i; j >= gap && arr[j] < arr[j - gap]; j -= gap)
{
swap(arr[j - gap], arr[j]);
}
gap /= 3;
}
}
}
int main()
{
int arr[] = {5, 4, 3, 2, 1, 0};
int n = sizeof(arr) / sizeof(arr[0]);
shellSort(arr, n);
for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
}
Last updated
Was this helpful?