Advertisement
Guest User

Ruby Bubble sort

a guest
Nov 15th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.41 KB | None | 0 0
  1. require 'byebug'
  2.  
  3. # Bubble sort
  4. def bubble_sort(a)
  5.   (a.size - 1).times do |j|
  6.     sorted = true
  7.     0.upto(a.size - j - 2) do |i|
  8.       if a[i] > a [i + 1]
  9.         a[i], a[i + 1] = a[i + 1], a[i]
  10.         sorted = false
  11.       end
  12.     end
  13.     return a if sorted
  14.   end
  15.  
  16.   return a
  17. end
  18.  
  19. a =  generate_array(16)
  20. puts a.join(', ')
  21.  
  22. # SORTING
  23. puts "Bubble sort\nО(n^2)", bubble_sort(a.clone).inspect
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement