-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell_sort.cpp
More file actions
41 lines (35 loc) · 731 Bytes
/
shell_sort.cpp
File metadata and controls
41 lines (35 loc) · 731 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <vector>
#include <utility>
/*
worst: O(n * n)
average: depends on gap sequence
best: O(n log n)
*/
template<typename Comparable>
void ShellSort(std::vector<Comparable>* outData)
{
for(int gap = outData->size() / 2; gap > 0; gap /= 2)
{
for(int i = gap; i < outData->size(); ++i)
{
Comparable tmp = std::move(outData->at(i));
int j = i;
for( ; j >= gap && tmp < outData->at(j - gap); j -= gap)
{
outData->at(j) = std::move(outData->at(j - gap));
}
outData->at(j) = std::move(tmp);
}
}
}
int main()
{
std::vector<int> a1{ 8, 6, 4, 4, 1, 9, 2, 78, 25, 45, 11, 35 };
ShellSort(&a1);
for(const auto& x : a1)
{
std::cout << x << ' ';
}
std::cout << '\n';
}