Sha256: 2c469b7e39e9bc9d6cc3e37340c694e5a26f419469a83e7f4a4b449d5de3003f

Contents?: true

Size: 712 Bytes

Versions: 1

Compression:

Stored size: 712 Bytes

Contents

# frozen_string_literal: true

require 'bubble_sort/version'

# module BubbleSort
module BubbleSort
  class Error < StandardError; end

  # Bubble sort an array IN-PLACE
  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
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bubble_sort-0.1.0 lib/bubble_sort.rb