module BubbleSort

module BubbleSort

Constants

VERSION

Public Class Methods

bubble_sort(array) click to toggle source

Bubble sort an array IN-PLACE

# File lib/bubble_sort.rb, line 10
def self.bubble_sort(array) # rubocop:disable Metrics/AbcSize
  raise TypeError, 'Given argument is not an array!' unless array.is_a? Array
  raise TypeError, 'Given array contains values which are not numbers!' unless array.all? do |value|
    value.is_a? Numeric
  end

  array[...-1].each_index do |index|
    to = array.length - (index + 1)
    array[...to].each_index do |index2|
      array[index2], array[index2 + 1] = array[index2 + 1], array[index2] if array[index2] > array[index2 + 1]
    end
  end
end