Class: Quicksort

Inherits:
Object
  • Object
show all
Defined in:
lib/qs.rb

Class Method Summary collapse

Class Method Details

.qs(array) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/qs.rb', line 2

def self.qs (array)
  if array.length <= 1
    return array
  end
  less = Array.new
  greater = Array.new
  pivot = array.delete_at(array.length/2)
  array.each do |x|
    if x < pivot
      less << x
    else
      greater << x
    end
  end
  return (self.qs(less) << pivot << self.qs(greater)).flatten.compact
end