From 7c95d2a96cb3de5b9a81c6d7ae1b9e40065721fa Mon Sep 17 00:00:00 2001 From: booboy Date: Sat, 4 May 2019 20:56:36 -0500 Subject: [PATCH] started a little bit of selection sorting algorithm --- algorithms/lib/algorithms/sorting.rb | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/algorithms/lib/algorithms/sorting.rb b/algorithms/lib/algorithms/sorting.rb index 4c8ca29..6399e0b 100644 --- a/algorithms/lib/algorithms/sorting.rb +++ b/algorithms/lib/algorithms/sorting.rb @@ -1,11 +1,27 @@ module Algorithms class Sorting - def selection(a, n) - end + def selection(a) + # a is an Array + # from: https://www.tutorialspoint.com/data_structures_algorithms/selection_sort_algorithm.htm + # Step 1 − Set MIN to location + # Step 2 − Search the minimum element in the list + # Step 3 − Swap with value at location MIN + # Step 4 − Increment MIN to point to next element + # Step 5 − Repeat until list is sorted - def bubble - end + a.each_with_index do |value, index| + # set current element as minimum + current_element = a[index] + next_element = a[index + 1] + if next_element.nil? == false && current_element > next_element + puts "Current Element: #{current_element}" + puts "Next Element: #{next_element}" + + end + end + + def insertion end