class Object

Public Instance Methods

bubblesort(array) click to toggle source

Bubble sort implementation in Ruby

# File lib/rbutils/sort/bubblesort.rb, line 3
def bubblesort(array)
  x = array.clone
  x.each_index do |i|
    (x.length-i-1).times do |j|
      if (x[j+1] < x[j])
        x[j], x[j+1] = x[j+1], x[j]
      end
    end
  end
  return x
end
quicksort(array, l, r) click to toggle source

Quick Sort implementation in Ruby

# File lib/rbutils/sort/quicksort.rb, line 3
def quicksort(array, l, r)
  if r == -1
    r = array.length - 1
  end

  if l == -1
    l = 0
  end

  if (r - l) < 1
    return array
  else
    pivot = array[l]
    i = l+1
    for j in l+1..r
        if array[j] < pivot
            array[j], array[i] = array[i], array[j]
            i += 1
        end
    end
    
    array[l], array[i-1] = array[i-1], array[l]
    array = quicksort(array, l, i-1)
    array = quicksort(array, i, r)
    return array
  end

end