Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
42 lines (36 sloc) 937 Bytes
# INTERRUPTED BUBBLE SORT
# Auhor: Stefan Armijo
# https://www.codeeval.com/open_challenges/158/
def arr_gen(line)
arr = line.delete("|").split.map(&:to_i)
end
def bubble_sort_x(arr)
x = arr.pop
x = x < arr.length ? x : arr.length
x.times do |n|
(arr.length - 1).times do |e|
(arr[e],arr[e +1] = arr[e + 1],arr[e]) if (arr[e] > arr[e +1])
end
end
puts arr.join(" ")
end
File.open(ARGV[0]).each_line do |line|
bubble_sort_x(arr_gen(line.chomp))
end
# think about edge cases:
#
# test file:
# 36 47 78 28 20 79 87 16 8 45 72 69 81 66 60 8 3 86 90 90 | 1
# 40 69 52 42 24 16 66 | 2
# 54 46 0 34 15 48 47 53 25 18 50 5 21 76 62 48 74 1 43 74 78 29 | 6
# 48 51 5 61 18 | 2
# 59 68 55 31 73 4 1 25 26 19 60 0 | 2
#
#
# what about:
# 38 22 95 10 84 | 0
# 89 37 29 17 05 83 75 88 22 82 | 10
# 0 1 2 0 0 0 0 2 0 0 | 5
# 89 22 55 83 92 75 01 84 92 22 | 600
# -5 -2 03 99 -1 0 0 99 -9 -8 | 4?
# ??????