Class: Array

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

Instance Method Summary collapse

Instance Method Details

#ordenar_eachObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/prct06/array.rb', line 35

def ordenar_each

  @resultado = self.map { |x| x }
    indice=0
          @resultado.each do |x|
                var = x
      i = indice
      indice2=indice + 1
    
                @resultado[indice2..@resultado.length-1].each do |y|
                    if var > y 
                          var = y
                          i = indice2
                    end
                    indice2+=1
                end
              @resultado[i] = x
              @resultado[indice] = var
              indice+=1
          end

         @resultado
end

#ordenar_forObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/prct06/array.rb', line 14

def ordenar_for

  @resultado = self.map { |x| x }
          for x in 0..self.count-1
                aux = @resultado[x]
      i = x

                for y in x + 1..self.count-1
                    if aux > @resultado[y] 
                          aux = @resultado[y]
                          i = y
                    end
                end
              
    @resultado[i] = @resultado[x]
                 @resultado[x] = aux
             end

        @resultado
end

#quick_sort(array) ⇒ Object



3
4
5
6
7
8
9
10
11
12
# File 'lib/prct06/array.rb', line 3

def quick_sort(array)
    return array if array.size <= 1

    pivot ||= array.shift

    left = array.select { |n| n < pivot }
    right = array.select { |n| n >= pivot }

    return [*quick_sort(left), pivot, *quick_sort(right)]
end