Skip to content

Commit

Permalink
used the each with index method instead of the earlier .times method …
Browse files Browse the repository at this point in the history
…due to linter errors
  • Loading branch information
imhilla committed May 12, 2020
1 parent cf67d66 commit 25a5bcc
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions bubble_sort.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
class Array
def bubble_sort
n = self.size
loop do
swapped = false
(n - 1).times do |i|
if self[i] > self[i + 1]
self[i], self[i + 1] = self[i + 1], self[i]
swapped = true
end
def bubble_sort(array)
loop do
swapped = false
array.each_with_index do |val, index|
next if index >= array.length - 1
if array[index] > array[index + 1]
array[index], array[index + 1] = array[index + 1], array[index]
swapped = true
end
break unless swapped
end
self
break unless swapped
end
pp array
end

def bubble_sort_by(array)
Expand All @@ -27,5 +25,5 @@ def bubble_sort_by(array)
end
break unless swapped
end
pp array
array
end

0 comments on commit 25a5bcc

Please sign in to comment.