mirror of
https://github.com/TheAlgorithms/Ruby
synced 2024-11-16 19:50:00 +01:00
Add gnome sort
This commit is contained in:
parent
14149d1eaf
commit
4dab82985e
2 changed files with 23 additions and 0 deletions
12
sorting/gnome_sort.rb
Normal file
12
sorting/gnome_sort.rb
Normal 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
|
11
sorting/gnome_sort_test.rb
Normal file
11
sorting/gnome_sort_test.rb
Normal 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
|
Loading…
Reference in a new issue