first commit
This commit is contained in:
32
sort_closet/code-sorting/ShellSort.cpp
Normal file
32
sort_closet/code-sorting/ShellSort.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
#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++;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user