2017-09-28 19:01:37 +02:00
|
|
|
def shell_sort(a)
|
2021-02-07 08:05:54 +01:00
|
|
|
n = a.length
|
|
|
|
h = 1
|
2017-09-28 19:01:37 +02:00
|
|
|
|
2021-02-07 08:05:54 +01:00
|
|
|
h = (3 * h) + 1 while h < n / 3
|
2017-09-28 19:01:37 +02:00
|
|
|
|
2021-02-07 08:05:54 +01:00
|
|
|
while h >= 1
|
2017-09-28 19:01:37 +02:00
|
|
|
# Logic of insertion sort with inrement steps of "h"
|
2021-02-07 08:05:54 +01:00
|
|
|
(h...n).each do |i|
|
|
|
|
j = i
|
|
|
|
while j >= h
|
|
|
|
if a[j - h] > a[j]
|
|
|
|
temp = a[j]
|
|
|
|
a[j] = a[j - h]
|
|
|
|
a[j - h] = temp
|
2017-09-28 19:01:37 +02:00
|
|
|
end
|
2021-02-07 08:05:54 +01:00
|
|
|
j -= h
|
2017-09-28 19:01:37 +02:00
|
|
|
end
|
|
|
|
end
|
2021-02-07 08:05:54 +01:00
|
|
|
h /= 3
|
2017-09-28 19:01:37 +02:00
|
|
|
end
|
|
|
|
|
2021-02-07 08:05:54 +01:00
|
|
|
a
|
2017-09-28 19:01:37 +02:00
|
|
|
end
|