Skip to content

Commit 0c832c0

Browse files
committed
Add Ruby insertion sort
1 parent 2ad2dec commit 0c832c0

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
def insertion_sort(input)
3+
input.size.times do |i|
4+
j = i-1
5+
curr_element = input[i]
6+
while j >= 0 && input[j] > curr_element do
7+
input[j+1] = input[j]
8+
j -= 1
9+
end
10+
input[j+1] = curr_element
11+
end
12+
end
13+
14+
input = [7, 6, 5, 9, 8, 4, 3, 1, 2, 0, 5]
15+
insertion_sort(input)
16+
puts input

0 commit comments

Comments
 (0)