bubble sort


See On Github

Data

Contributor

Generic placeholder thumbnail

by majjoha

in ruby

Source Code

def bubble_sort(array)
  n = array.length - 2

  while true do
    swapped = false
    (0..n).each_with_index do |item, i|
      if array[i] > array[i+1]
        array[i], array[i+1] = array[i+1], array[i]
        swapped = true
      end
    end
    break unless swapped
  end
  array
end
require 'rspec'
require './bubble_sort'

describe '#bubble_sort' do
  context 'when the array is not sorted' do
    let(:array) { ['S','O','R','T','E','X','A','M','P','L','E'] }

    it 'orders the array' do
      sorted_array = bubble_sort(array)
      expect(sorted_array).to eq array.sort
    end
  end

  context 'when the array is empty' do
    let(:array) { [] }

    it 'returns an empty array' do
      sorted_array = bubble_sort(array)
      expect(sorted_array).to eq []
    end
  end
end