forked from Vishal1003/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShell Sort.cpp
More file actions
52 lines (39 loc) · 854 Bytes
/
Shell Sort.cpp
File metadata and controls
52 lines (39 loc) · 854 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
48
49
50
51
52
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
vector<int> shellSort(vector<int>V)
{
for(int gap = V.size() / 2; gap > 0; gap /= 2)
{
for(int i = gap; i < V.size(); i += 1)
{
int temp = V[i];
int j;
for(j = i; j >= gap && V[j - gap] > temp; j -= gap)
V[j] = V[j - gap];
V[j] = temp;
}
}
return V;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int N;
cin>>N; //size of array
vector<int>V;
for(int i = 0; i < N; i++) //taking array inputs
{
int inp;
cin>>inp;
V.pb(inp);
}
vector<int>res = shellSort(V);
//final sorted array
for(int i = 0; i < res.size(); i++)
cout<<res[i]<<" ";
cout<<endl;
return 0;
}