-
Notifications
You must be signed in to change notification settings - Fork 98
/
ShellSort.cpp
51 lines (46 loc) · 1.44 KB
/
ShellSort.cpp
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
42
43
44
45
46
47
48
49
50
51
#include <vector>
#include <algorithm>
#include <iostream>
/**
* This function implements the Shell Sorting Algorithm.
*
* @note Knuth sequence was used.
*
* @param v Vector to be sorted.
*/
void shellSort(std::vector<int> &v) {
int gap;
// calculate the first gap, based on Knuth sequence
for(gap = 1; gap < (v.end() - v.begin()); gap = 3 * gap + 1);
while(gap > 0) { // change to while(gap)
// descrease the gap
gap = (gap - 1)/3;
// pass by elements to swap then if necessary
for(size_t i = 0; v.begin() + gap + i < v.end(); i++) {
if(*(v.begin() + gap + i) < *(v.begin() + i)) {
std::swap(*(v.begin() + gap + i), *(v.begin() + i));
// in case some element was sawpped, it will try swap the elements before, maintaining the gap
for(int j = i - gap; j >= 0; j -= gap) {
if(*(v.begin() + gap + j) < *(v.begin() + j)) { std::swap(*(v.begin() + gap + j), *(v.begin() + j)); }
}
}
}
}
}
/**
* Simple print function.
*/
void printVec(std::vector<int> v) {
for(int i = 0; i < v.size(); i++) std::cout << v[i] << " ";
std::cout << std::endl;
}
/**
* Driver code.
*/
int main() {
std::vector<int> v = {23, -10, 20, -11, 12, -6, 7, 299, 9, -1, 37, 51, 0};
shellSort(v);
std::cout << "Sorted: ";
printVec(v);
return 0;
}