Add gnome sort

This commit is contained in:
AMQOR Merouane 2023-06-21 17:03:58 +01:00
parent 14149d1eaf
commit 4dab82985e
2 changed files with 23 additions and 0 deletions

12
sorting/gnome_sort.rb Normal file
View file

@ -0,0 +1,12 @@
def gnome_sort(arr)
i = 0
while i < arr.length
if i == 0 || arr[i] >= arr[i - 1]
i += 1
else
arr[i], arr[i - 1] = arr[i - 1], arr[i]
i -= 1
end
end
arr
end

View file

@ -0,0 +1,11 @@
require 'minitest/autorun'
require_relative './sort_tests'
require_relative './gnome_sort'
class TestGnomeSort < Minitest::Test
include SortTests
def sort(input)
gnome_sort(input)
end
end