Skip to content

Commit 163a829

Browse files
authored
Create bubble_sort_by.rb
1 parent a5114c8 commit 163a829

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
def bubble_sort_by array
2+
is_sorted = false
3+
digits = (0..array.length - 2)
4+
5+
until is_sorted
6+
is_sorted = true
7+
digits.each do |i|
8+
block = yield(array[i], array[i + 1])
9+
if block > 0
10+
array[i], array[i+1] = array[i+1], array[i]
11+
is_sorted = false
12+
end
13+
end
14+
end
15+
print array
16+
end
17+
18+
bubble_sort_by(["hi","hello","hey"]) {|left,right| left.length - right.length}

0 commit comments

Comments
 (0)