forked from Vatsalparsaniya/Data-Structure
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShell_sort.cpp
More file actions
47 lines (35 loc) · 692 Bytes
/
Copy pathShell_sort.cpp
File metadata and controls
47 lines (35 loc) · 692 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
42
43
44
45
46
47
#include <iostream>
#include <vector>
using namespace std;
template<typename T>
void Shellsort(vector<T> & vec)
{
size_t j;
T temp;
for(size_t aux = vec.size() / 2; aux > 0; aux /= 2)
{
for(size_t i = aux; i < vec.size(); ++i)
{
temp = vec[i];
for(j = i; j >= aux and vec[j - aux] > temp; j -= aux)
vec[j] = vec[j - aux];
vec[j]=temp;
}
}
}
int main()
{
int array_size;
cout<<"Array size: ";
cin>>array_size;
vector<int> vec(array_size);
cout<<"Array elements: ";
for(int i = 0; i < array_size; i++)
cin>>vec[i];
Shellsort(vec);
cout<<"Sorted array: ";
for(int i = 0; i < array_size; i++)
cout<<vec[i]<<' ';
cout<<endl;
return 0;
}