Bubble sort

Ruby

Public Domain

Learning implementation of bubble sort in Ruby

Download (right click, save as, rename as appropriate)

Embed

 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
#!/usr/bin/ruby

#array = [48,61,51,39,89,23,51,85,83,70,13,89,18,90,72,7,0,71,40,91,1,5,33,2,44,88,5,14,19,21,0,11,15,24,41,3,7,3,9,0,5,9,2,1,2,23,4]
array = []

330.times do |n|
  array[n] = rand(99)
end


count = array.length

while count > 2 do
  (count-1).times do |n|
    if array[n].to_i > array[n+1].to_i
      array[n], array[n+1] = array[n+1], array[n]
    end
  end
count -= 1
#p array
array.each do |n|
  print n
end
print "\n"

end