From d13fdb99f2a00044077d0ae85236c24336d51372 Mon Sep 17 00:00:00 2001 From: vzvu3k6k Date: Wed, 17 Feb 2021 02:45:36 +0900 Subject: [PATCH] Add tests for selection sort --- sorting/selection_sort.rb | 10 +++++++--- sorting/selection_sort_test.rb | 11 +++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 sorting/selection_sort_test.rb diff --git a/sorting/selection_sort.rb b/sorting/selection_sort.rb index b32c944..62730af 100644 --- a/sorting/selection_sort.rb +++ b/sorting/selection_sort.rb @@ -11,8 +11,12 @@ def selection_sort(array) array[i], array[smallest] = array[smallest], array[i] if i != smallest i += 1 end + array end -arr = [9, 8, 3, 1, 2, 55, 68, 48].shuffle # We have taken a rondom example and also shuffling it -selection_sort(arr) -puts "Sorted array is: #{arr.inspect}" +if $0 == __FILE__ + puts 'Enter a list of numbers separated by space' + + list = gets.split.map(&:to_i) + p selection_sort(list) +end diff --git a/sorting/selection_sort_test.rb b/sorting/selection_sort_test.rb new file mode 100644 index 0000000..eef1a39 --- /dev/null +++ b/sorting/selection_sort_test.rb @@ -0,0 +1,11 @@ +require 'minitest/autorun' +require_relative './sort_tests' +require_relative './selection_sort' + +class TestSelectionSort < Minitest::Test + include SortTests + + def sort(input) + selection_sort(input) + end +end