Class: Array

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

Overview

Open the Array class

Instance Method Summary collapse

Instance Method Details

#sort_eachArray

Returns the array sorted

Returns:

  • (Array)

    the array sorted



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/TDD/sort.rb', line 20

def sort_each
     # Deep copy
     aux = Marshal.load(Marshal.dump(self))
     result = []

     # Each iteration finds the minimun from aux,
     # deletes it and add it to result
     self.length.times do |i|
          m = aux.each_with_index.min
          aux.delete_at(m[1])
          result << m[0]
     end
     
     return result
end

#sort_forArray

Returns the array sorted

Returns:

  • (Array)

    the array sorted



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/TDD/sort.rb', line 6

def sort_for
     # Deep copy
     r = Marshal.load(Marshal.dump(self))
     for i in (0...r.length)
          for j in (0...(r.length-i-1))
               r[j], r[j+1] = r[j+1], r[j] if(r[j] > r[j+1])
          end
     end

     return r
end