module Algorithmable::Sort::Utils

Public Instance Methods

exchange(from, to, collection) click to toggle source

@param [Integer] from @param [Integer] to @param [Array] collection @return [Object]

# File lib/algorithmable/sort/utils.rb, line 47
def exchange(from, to, collection)
  return collection if from == to
  local_from = collection[from]
  collection[from] = collection[to]
  collection[to] = local_from
  collection
end
partition(a, bottom, top) click to toggle source

@param [Integer] a @param [Integer] bottom @param [Array] top @return [Integer] Integer representing new pivot location

# File lib/algorithmable/sort/utils.rb, line 8
def partition(a, bottom, top)
  i = bottom
  j = top.succ
  v = a[bottom]
  loop do
    while a[i += 1] < v
      break if i == top
    end
    while v < a[j -= 1]
      break if j == bottom
    end
    break if i >= j
    cur_i = a[i]
    a[i] = a[j]
    a[j] = cur_i
  end
  cur_bottom = a[bottom]
  a[bottom] = a[j]
  a[j] = cur_bottom
  j
end
swap(collection, i) click to toggle source

@param [Integer] collection @param [Integer] i @return [Array] collection

# File lib/algorithmable/sort/utils.rb, line 58
def swap(collection, i)
  current = collection[i]
  collection[i] = collection[i + 1]
  collection[i + 1] = current
  collection
end