Ruby Algorithms - Bubblesort - Women Who Code.
Resources for women who code
women who code, girls who code, ladies who code
52048
post-template-default,single,single-post,postid-52048,single-format-standard,qode-core-1.0.1,ajax_fade,page_not_loaded,,capri child-child-ver-1.0.0,capri-ver-1.6, vertical_menu_with_scroll,smooth_scroll,grid_1200,blog_installed,wpb-js-composer js-comp-ver-4.12.1,vc_responsive
 

Ruby Algorithms – Bubblesort

Ruby Algorithms – Bubblesort

Bubble Sort:

 def bubble_sort(array)
  n = array.length 
  
  loop 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
    
    break if not swapped 
  end 
  
  array 
end

a = [7, 1, 5, 9, 2, 0, 263, 14, 422, 1, 2]
p bubble_sort(a)

Bubble sort algorithm implementation in Ruby

 
No Comments

Post a Comment