forked from thuva4/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShellSort.rb
More file actions
33 lines (27 loc) · 683 Bytes
/
Copy pathShellSort.rb
File metadata and controls
33 lines (27 loc) · 683 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
#Shell Sort implementation(Diminishing Increment Sort)
#Time-complexity: O(n^2), In-place
#will be using Knuth series :3n+1
def shell_sort(a)
n=a.length
h=1
while (h<n/3) #for computing increment factor "h"
h= (3*h)+1
end
while h>=1
# Logic of insertion sort with inrement steps of "h"
for i in h...n
j=i
while j>=h
if a[j-h]>a[j]
temp=a[j]
a[j]=a[j-h]
a[j-h]=temp
end
j-=h
end
end
h/=3
end
return a
end
puts(shell_sort([0,5,4,7,1,8,9,3,7,1,4,2,8,6]))