class Sommelier::DescendingInsertionSortArray

An extension of the ruby built-in Array class with an optimization for insertion sort. This class assumes the array elements are sorted in descending order.

Public Instance Methods

sorted_insert(item) click to toggle source

Insert `item` just before the element with the highest value that is lower than the value of `item`

@param item [Object] any object that responds to `>`

# File lib/sommelier/descending_insertion_sort_array.rb, line 10
def sorted_insert(item)
  insertion_index = (0...size).bsearch(&search_proc(item))
  insert(insertion_index || length, item)
end

Private Instance Methods

search_proc(item) click to toggle source
# File lib/sommelier/descending_insertion_sort_array.rb, line 17
def search_proc(item)
  ->(index) { item > self[index] }
end