ssdcua
Last Updated: May 19, 2018
·
755
· 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

6 Responses
Add your response

29293
Article 2371334 1ae7450d000005dc 684 634x421

Oh thanks for bubble sort in ruby really thanks man

9 months ago ·
29587

thanks

5 months ago ·
29588

bubble sort mark

5 months ago ·
29728
12 1510738735134

what is the advantage of this over single and multi-line commenting by clicking the line number, or selecting multiple lines then pressing "command(or ctrl) + /"?

Read manga online for free in high quality and most full at MangaFox: https://manga-fox.com/

3 months ago ·
29739
Wp themes india   copy

Thanks for sharing bubble sort in ruby It will be more helpful for us as best webdesign company in India

3 months ago ·
29914

Thanks for share with me this information http://www.geometrydash.me/

28 days ago ·