Files
algo2025/sort_closet/code-sorting/ShellSort.cpp
2025-12-18 16:00:22 +08:00

33 lines
781 B
C++

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
/*
* function : implement shell sort
* param nums : the vector to be sorted
* param comp_count : count of comparisons
* param move_count : count of moves
* return : ---
*/
void ShellSort(vector<int> &nums, long long &comp_count, long long &move_count)
{
int n = nums.size();
for (int gap = n / 2; gap > 0; gap /= 2)
{
for (int i = gap; i < n; i += 1)
{
int temp = nums[i];
int j;
for (j = i; j >= gap && (comp_count++, nums[j - gap] > temp); j -= gap)
{
nums[j] = nums[j - gap];
move_count++;
}
nums[j] = temp;
move_count++;
}
}
}