Class: Array

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

Instance Method Summary collapse

Instance Method Details

#bubble_sortObject



2
3
4
5
6
7
8
9
10
# File 'lib/nyuudou/extension/array.rb', line 2

def bubble_sort
  ary = self.dup
  (0...(ary.length - 1)).each { |i|
    ((i+1)...(ary.length)).reverse_each { |j|
      ary[j-1], ary[j] = ary[j], ary[j-1] if ary[j-1] > ary[j]
    }
  }
  ary
end

#bubble_sort!Object



12
13
14
15
16
17
18
19
# File 'lib/nyuudou/extension/array.rb', line 12

def bubble_sort!
  (0...(self.length - 1)).each { |i|
    ((i+1)...(self.length)).reverse_each { |j|
      self[j-1], self[j] = self[j], self[j-1] if self[j-1] > self[j]
    }
  }
  self
end

#selection_sortObject



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/nyuudou/extension/array.rb', line 21

def selection_sort
  ary = self.dup
  (0...(ary.length - 1)).each { |i|
    min = i
    ((i+1)...(ary.length)).each { |j|
      min = j if ary[j] < ary[min]
    }
    ary[i], ary[min] = ary[min], ary[i]
  }
  ary
end

#selection_sort!Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/nyuudou/extension/array.rb', line 33

def selection_sort!
  (0...(self.length - 1)).each { |i|
    min = i
    ((i+1)...(self.length)).each { |j|
      min = j if self[j] < self[min]
    }
    self[i], self[min] = self[min], self[i]
  }
  self
end