shell sort


See On Github

Data

Contributor

Generic placeholder thumbnail

by majjoha

in ruby

Source Code

def shell_sort(array)
  h = array.size / 2
  while h > 0 do
    (h..array.length).each_with_index do |item, j|
      val = array[j]
      i = j
      while (i >= h && array[i-h] > val)
        array[i] = array[i-h]
        i -= h
      end
      array[i] = val
    end
    h /= 2
  end
  array
end
require 'rspec'
require './shell_sort'

describe '#shell_sort' do
  context 'Not sorted' do
    let(:array) { ['S','O','R','T','E','X','A','M','P','L','E'] }

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

  context 'Empty array' do
    let(:array) { @array = [] }

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