From 4dab82985e862624cf6e02b04a5ac37a3c7f7889 Mon Sep 17 00:00:00 2001 From: AMQOR Merouane Date: Wed, 21 Jun 2023 17:03:58 +0100 Subject: [PATCH] Add gnome sort --- sorting/gnome_sort.rb | 12 ++++++++++++ sorting/gnome_sort_test.rb | 11 +++++++++++ 2 files changed, 23 insertions(+) create mode 100644 sorting/gnome_sort.rb create mode 100644 sorting/gnome_sort_test.rb diff --git a/sorting/gnome_sort.rb b/sorting/gnome_sort.rb new file mode 100644 index 0000000..0e6768e --- /dev/null +++ b/sorting/gnome_sort.rb @@ -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 diff --git a/sorting/gnome_sort_test.rb b/sorting/gnome_sort_test.rb new file mode 100644 index 0000000..992ee2a --- /dev/null +++ b/sorting/gnome_sort_test.rb @@ -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