Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/array/sort/helper.rb,
lib/array/sort/heap_sort.rb,
lib/array/sort/merge_sort.rb,
lib/array/sort/quick_sort.rb,
lib/array/sort/bubble_sort.rb,
lib/array/sort/insertion_sort.rb

Instance Method Summary collapse

Instance Method Details

#bubble_sort(&block) ⇒ Object



4
5
6
# File 'lib/array/sort/bubble_sort.rb', line 4

def bubble_sort(&block)
  dup.bubble_sort!(&block)
end

#bubble_sort!(&block) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/array/sort/bubble_sort.rb', line 8

def bubble_sort!(&block)
  return self if length <= 1
  n = length
  loop do
    new_n = bubble_sort_check(n, &block)
    n = new_n
    break if n.zero?
  end
  self
end

#bubble_sort_by(&block) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/array/sort/bubble_sort.rb', line 19

def bubble_sort_by(&block)
  if block_given?
    dup.bubble_sort_by!(&block)
  else
    to_enum :bubble_sort_by
  end
end

#bubble_sort_by!(&_block) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/array/sort/bubble_sort.rb', line 27

def bubble_sort_by!(&_block)
  if block_given?
    bubble_sort! do |a, b|
      yield(a) <=> yield(b)
    end
  else
    to_enum :bubble_sort_by!
  end
end

#heap_sort(&block) ⇒ Object



4
5
6
# File 'lib/array/sort/heap_sort.rb', line 4

def heap_sort(&block)
  dup.heap_sort!(&block)
end

#heap_sort!(&block) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/array/sort/heap_sort.rb', line 8

def heap_sort!(&block)
  return self if length <= 1
  heapify(&block)
  (length - 1).downto(1) do |end_|
    swap(end_, 0)
    sift_down(0, end_ - 1, &block)
  end
  self
end

#heap_sort_by(&block) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/array/sort/heap_sort.rb', line 18

def heap_sort_by(&block)
  if block_given?
    dup.heap_sort_by!(&block)
  else
    to_enum :heap_sort_by
  end
end

#heap_sort_by!(&_block) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/array/sort/heap_sort.rb', line 26

def heap_sort_by!(&_block)
  if block_given?
    heap_sort! do |a, b|
      yield(a) <=> yield(b)
    end
  else
    to_enum :heap_sort_by!
  end
end

#insertion_sort(&block) ⇒ Object



4
5
6
# File 'lib/array/sort/insertion_sort.rb', line 4

def insertion_sort(&block)
  dup.insertion_sort!(&block)
end

#insertion_sort!(&block) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/array/sort/insertion_sort.rb', line 8

def insertion_sort!(&block)
  return self if length <= 1
  (1...length).each do |i|
    i.downto(1) do |j|
      break unless sort_compare(self[j - 1], self[j], &block) == 1
      swap(j - 1, j)
    end
  end
  self
end

#insertion_sort_by(&block) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/array/sort/insertion_sort.rb', line 19

def insertion_sort_by(&block)
  if block_given?
    dup.insertion_sort_by!(&block)
  else
    to_enum :insertion_sort_by
  end
end

#insertion_sort_by!(&_block) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/array/sort/insertion_sort.rb', line 27

def insertion_sort_by!(&_block)
  if block_given?
    insertion_sort! do |a, b|
      yield(a) <=> yield(b)
    end
  else
    to_enum :insertion_sort_by!
  end
end

#merge_sort(&block) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/array/sort/merge_sort.rb', line 4

def merge_sort(&block)
  return dup if length <= 1
  divided_parts = merge_sort_divide
  part_a_sorted = divided_parts[0].merge_sort
  part_b_sorted = divided_parts[1].merge_sort
  merge_sort_merge part_a_sorted, part_b_sorted, &block
end

#merge_sort!(&block) ⇒ Object



12
13
14
# File 'lib/array/sort/merge_sort.rb', line 12

def merge_sort!(&block)
  become_clone_of merge_sort(&block)
end

#merge_sort_by(&_block) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/array/sort/merge_sort.rb', line 16

def merge_sort_by(&_block)
  if block_given?
    merge_sort do |a, b|
      yield(a) <=> yield(b)
    end
  else
    to_enum :merge_sort_by
  end
end

#merge_sort_by!(&block) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/array/sort/merge_sort.rb', line 26

def merge_sort_by!(&block)
  if block_given?
    become_clone_of merge_sort_by(&block)
  else
    to_enum :merge_sort_by!
  end
end

#quick_sort(&block) ⇒ Object



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

def quick_sort(&block)
  return dup if length <= 1
  left, right = self[1..-1].partition { |y| sort_compare(first, y, &block).positive? }
  left.quick_sort + [first] + right.quick_sort
end

#quick_sort!(&block) ⇒ Object



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

def quick_sort!(&block)
  become_clone_of quick_sort(&block)
end

#quick_sort_by(&_block) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/array/sort/quick_sort.rb', line 14

def quick_sort_by(&_block)
  if block_given?
    quick_sort do |a, b|
      yield(a) <=> yield(b)
    end
  else
    to_enum :quick_sort_by
  end
end

#quick_sort_by!(&block) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/array/sort/quick_sort.rb', line 24

def quick_sort_by!(&block)
  if block_given?
    become_clone_of quick_sort_by(&block)
  else
    to_enum :quick_sort_by!
  end
end

#swap(index1, index2) ⇒ Array

Swap two elements in the array, and returns self.

Parameters:

  • index1 (Integer)

    The index of the first element.

  • index2 (Integer)

    The index of the second element.

Returns:

Raises:

  • (ArgumentError)


9
10
11
12
13
# File 'lib/array/sort/helper.rb', line 9

def swap(index1, index2)
  raise ArgumentError, 'Index must be an integer.' unless index1.is_a?(Integer) && index2.is_a?(Integer)
  self[index1], self[index2] = self[index2], self[index1] if index1 != index2
  self
end