ssdcua
Last Updated: November 22, 2017
·
127
· webcoderph

Bubble Sort in Ruby

def bubble_sort(array)
   n = array.length
   swapped = true
   while swapped do
     swapped = false
     (n - 1).times do |i|
       if array[i] > array[i + 1]
         array[i], array[i + 1] = array[i + 1], array[i]
     swapped = true
       end
     end
   end
   array 
end

arr = [2,3,100,3,5,4,10,7]

p bubble_sort(arr) # => [2, 3, 3, 4, 5, 7, 10, 100]

Say Thanks
Respond

1 Response
Add your response

29293
Article 2371334 1ae7450d000005dc 684 634x421

Oh thanks for bubble sort in ruby really thanks man

3 months ago ·