-
Notifications
You must be signed in to change notification settings - Fork 0
/
223.rb
53 lines (46 loc) · 1.17 KB
/
223.rb
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
# bindind.pry
# Sorts an array using comb sort.
# def comb_sort(array, compare = lambda { |a, b| a <=> b })
# gap = array.length
# shrink_factor = 1.30
# swapped = false
# d = Hash.new
# d.default = []
# while gap > 1 || swapped
# if gap > 1
# gap = (gap / shrink_factor).floor
# end
# swapped = false
# i = 0
# while gap + i < array.length
# if compare.call(array[i], array[i + gap]) > 0
# array[i], array[i + gap] = array[i + gap], array[i]
# swapped = true
# d[gap] = d[gap] << true
# end
# i += 1
# end
# end
# array.join(',')
# # .size - d.min_by {|k,v| k}.first.to_i
# end
File.open("223.txt").each_line do |line|
a = line.split(' ').map(&:to_i)
swap = true
size = a.length
gap = 1.25
d = Hash.new
d.default = []
while size >= 1 && swap
size = size / gap
(a.length - size.to_i).times do |i|
if a[i] > a[i + size]
a[i],a[i + size] = a[i + size], a[i]
d[size.to_i] = d[size.to_i] << true
swap
end
end
!swap
end
puts (d.min_by {|k,v| k}.first.to_i..a.length - 1).size
end