Module: Kata::Algorithms::Sorting
- Defined in:
- lib/kata/algorithms/sorting.rb
Class Method Summary collapse
-
.insertion_sort(array_to_sort) ⇒ Object
The same array sorted using insertion sort.
Class Method Details
.insertion_sort(array_to_sort) ⇒ Object
Returns the same array sorted using insertion sort.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/kata/algorithms/sorting.rb', line 7 def self.insertion_sort(array_to_sort) return array_to_sort if array_to_sort.nil? || array_to_sort.size == 0 || array_to_sort.size == 1 i = 1 while i < array_to_sort.size value = array_to_sort[i] j = i - 1 while j >= 0 && array_to_sort[j] >= value array_to_sort[j + 1] = array_to_sort[j] j -= 1 end array_to_sort[j+1] = value i += 1 end array_to_sort end |