Class: Array

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

Instance Method Summary collapse

Instance Method Details

#bubble_sortObject



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/food/sort.rb', line 30

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

#bubble_sort!Object



42
43
44
# File 'lib/food/sort.rb', line 42

def bubble_sort!()
  self.replace(self.bubble_sort())
end

#bubble_sort_impObject



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/food/sort.rb', line 14

def bubble_sort_imp()
  clone_array = self.clone
  
  for i in 1...clone_array.size
      for j in 0...(clone_array.size - i)
          clone_array.swap!(j, j+1) if (clone_array[j] > clone_array[j+1])
      end
  end
  
  clone_array
end

#bubble_sort_imp!Object



26
27
28
# File 'lib/food/sort.rb', line 26

def bubble_sort_imp!()
  self.replace(self.bubble_sort_imp())
end

#merge_sortObject



46
47
48
# File 'lib/food/sort.rb', line 46

def merge_sort()
  split_array(self)
end

#merge_sort_impObject



50
51
52
# File 'lib/food/sort.rb', line 50

def merge_sort_imp()
  split_array(self)
end

#swap(i, j) ⇒ Object

Swap changes elements in position!



4
5
6
7
8
# File 'lib/food/sort.rb', line 4

def swap(i, j)
  clone_array = self.clone
  clone_array[i], clone_array[j] = clone_array[j], clone_array[i]
  clone_array
end

#swap!(i, j) ⇒ Object



10
11
12
# File 'lib/food/sort.rb', line 10

def swap!(i, j)
  self[i], self[j] = self[j], self[i]
end