array-sort 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 19712ae85993b11b3c6aa80b1b12557b3fe44081c736588fdb95491c888f9465
4
- data.tar.gz: 52adc3ab55c79a36af340b7ba0276a3ea31a759f6e93224dc3496d8c13d34d4e
3
+ metadata.gz: 91c86c745cc5568df53cec34a27f8da0a3b3b24899d0016150b794998b3da1c5
4
+ data.tar.gz: d63474b2f370ef3454ea7978acb838bf8901e51caf30f668dc9f2392f600befc
5
5
  SHA512:
6
- metadata.gz: 2f001fcef739c8843ea6b6d66813123d5c51c8c8ba2bc9d7ba633859914270671701087cc93a85ec7a7591582eab0d7e757c1be3a1d3a77fc14ea183dd56174e
7
- data.tar.gz: 64cafd15ee8abe2385debadd6f95659e2a7b35a8f7dfd9c61799ba1532baa62f24775089d505937ef36e4277be1c404135cee88cb4d55cef2624c003e9a307fc
6
+ metadata.gz: af69d008cb08440a894f1ee58583e565dcf4994a5825fd0f57415cbd8aea1fc90bc493c47d73be7e0c5304a8651fa7cd003fbda1a22d97ac3c16e658193ebdc4
7
+ data.tar.gz: 4dc3acb9501663a583d921c9fef4a50074eace63b1b67ebdc1eead7507e0ffef05c78ed60047fa796c4538e937e8776c71ecd7cd52093815f80d17fb5405af9c
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- array-sort (0.1.0)
4
+ array-sort (0.1.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # ArraySort
2
2
 
3
- ArraySort adds methods in the Ruby `Array` class that provides sorting methods using a few sorting algorithms.
3
+ ArraySort adds methods in the Ruby Array class that provides sorting methods using popular sorting algorithms.
4
4
 
5
5
  Currently, the following sorting algorithms are implemented:
6
6
  * Bubble sort _(stable)_
@@ -9,8 +9,8 @@ Currently, the following sorting algorithms are implemented:
9
9
  * Merge sort _(stable)_
10
10
  * Quicksort _(unstable)_
11
11
 
12
- Note that this gem does not overwrite `Array#sort`, `Array#sort!`, `Array#sort_by`, `Array#sort_by!`. Calling those
13
- methods will invoke the native sorting methods, which uses in-place quicksort algorithm and is unstable.
12
+ Note that this gem does not overwrite `Array#sort`, `Array#sort!`, `Array#sort_by`, or `Array#sort_by!`. Calling those
13
+ methods will invoke the native sorting methods, which use in-place quicksort algorithm and are unstable.
14
14
 
15
15
  ## Installation
16
16
 
@@ -40,7 +40,7 @@ change the names of the sorting methods to the following corresponding methods o
40
40
  * Quicksort: `quick_sort`, `quick_sort!`, `quick_sort_by`, `quick_sort_by!`
41
41
 
42
42
  See the official [Ruby documentation](https://ruby-doc.org/core-2.5.0/Array.html#method-i-sort) on how to use the native
43
- sorting methods of `Array`.
43
+ sorting methods of Array.
44
44
 
45
45
  For example, to sort an array using insertion sort:
46
46
 
@@ -10,8 +10,8 @@ Gem::Specification.new do |spec|
10
10
  spec.authors = ['Leo Liang']
11
11
  spec.email = ['developer@leoliang.com']
12
12
 
13
- spec.summary = 'Implementations of different sorting algorithms for Arrays.'
14
- spec.description = 'Implements sorting algorithms such as merge sort, bubble sort, insertion sort, etc. for Arrays.'
13
+ spec.summary = 'Implementations of popular sorting algorithms for Arrays.'
14
+ spec.description = 'Implements sorting algorithms such as merge sort, bubble sort, heap sort, etc. for Arrays.'
15
15
  spec.homepage = 'https://github.com/foxhatleo/array-sort'
16
16
  spec.license = 'MIT'
17
17
 
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ #
3
4
  require 'array/sort/version'
4
5
  require 'array/sort/helper'
5
6
  require 'array/sort/merge_sort'
@@ -1,10 +1,32 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Array
4
+ # Returns a new array created by sorting +self+, using bubble sort algorithm.
5
+ #
6
+ # Comparisons for the sort will be done using the <=> operator or using an optional code block.
7
+ #
8
+ # The block must implement a comparison between +a+ and +b+ and return an integer less than 0 when +b+ follows +a+,
9
+ # +0+ when +a+ and +b+ are equivalent, or an integer greater than 0 when +a+ follows +b+.
10
+ #
11
+ # The result is guaranteed to be stable. When the comparison of two elements returns +0+, the order of the elements
12
+ # will be preserved.
13
+ #
14
+ # @return [Array] the sorted array
4
15
  def bubble_sort(&block)
5
16
  dup.bubble_sort!(&block)
6
17
  end
7
18
 
19
+ # Sorts +self+ in place, using bubble sort algorithm.
20
+ #
21
+ # Comparisons for the sort will be done using the <=> operator or using an optional code block.
22
+ #
23
+ # The block must implement a comparison between +a+ and +b+ and return an integer less than 0 when +b+ follows +a+,
24
+ # +0+ when +a+ and +b+ are equivalent, or an integer greater than 0 when +a+ follows +b+.
25
+ #
26
+ # The result is guaranteed to be stable. When the comparison of two elements returns +0+, the order of the elements
27
+ # will be preserved.
28
+ #
29
+ # @return [Array] +self+
8
30
  def bubble_sort!(&block)
9
31
  return self if length <= 1
10
32
  n = length
@@ -16,6 +38,16 @@ class Array
16
38
  self
17
39
  end
18
40
 
41
+ # Returns a new array created by sorting +self+ with bubble sort algorithm, using a set of keys generated by mapping
42
+ # the values in self through the given block.
43
+ #
44
+ # The result is guaranteed to be stable. When the comparison of two elements returns +0+, the order of the elements
45
+ # will be preserved.
46
+ #
47
+ # If no block is given, an Enumerator is returned instead.
48
+ #
49
+ # @return [Array] if a block is given, the sorted array
50
+ # @return [Enumerator] if no block is given, an Enumerator
19
51
  def bubble_sort_by(&block)
20
52
  if block_given?
21
53
  dup.bubble_sort_by!(&block)
@@ -24,6 +56,16 @@ class Array
24
56
  end
25
57
  end
26
58
 
59
+ # Sorts +self+ in place with bubble sort algorithm, using a set of keys generated by mapping the values in self
60
+ # through the given block.
61
+ #
62
+ # The result is guaranteed to be stable. When the comparison of two elements returns +0+, the order of the elements
63
+ # will be preserved.
64
+ #
65
+ # If no block is given, an Enumerator is returned instead.
66
+ #
67
+ # @return [Array] if a block is given, the sorted array
68
+ # @return [Enumerator] if no block is given, an Enumerator
27
69
  def bubble_sort_by!(&_block)
28
70
  if block_given?
29
71
  bubble_sort! do |a, b|
@@ -36,6 +78,7 @@ class Array
36
78
 
37
79
  private
38
80
 
81
+ # @private
39
82
  def bubble_sort_check(length_checking, &block)
40
83
  new_n = 0
41
84
  (1...length_checking).each do |i|
@@ -1,10 +1,32 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Array
4
+ # Returns a new array created by sorting +self+, using heap sort algorithm.
5
+ #
6
+ # Comparisons for the sort will be done using the <=> operator or using an optional code block.
7
+ #
8
+ # The block must implement a comparison between +a+ and +b+ and return an integer less than 0 when +b+ follows +a+,
9
+ # +0+ when +a+ and +b+ are equivalent, or an integer greater than 0 when +a+ follows +b+.
10
+ #
11
+ # The result is not guaranteed to be stable. When the comparison of two elements returns +0+, the order of the
12
+ # elements is unpredictable.
13
+ #
14
+ # @return [Array] the sorted array
4
15
  def heap_sort(&block)
5
16
  dup.heap_sort!(&block)
6
17
  end
7
18
 
19
+ # Sorts +self+ in place, using heap sort algorithm.
20
+ #
21
+ # Comparisons for the sort will be done using the <=> operator or using an optional code block.
22
+ #
23
+ # The block must implement a comparison between +a+ and +b+ and return an integer less than 0 when +b+ follows +a+,
24
+ # +0+ when +a+ and +b+ are equivalent, or an integer greater than 0 when +a+ follows +b+.
25
+ #
26
+ # The result is not guaranteed to be stable. When the comparison of two elements returns +0+, the order of the
27
+ # elements is unpredictable.
28
+ #
29
+ # @return [Array] +self+
8
30
  def heap_sort!(&block)
9
31
  return self if length <= 1
10
32
  heapify(&block)
@@ -15,6 +37,16 @@ class Array
15
37
  self
16
38
  end
17
39
 
40
+ # Returns a new array created by sorting +self+ with heap sort algorithm, using a set of keys generated by mapping the
41
+ # values in self through the given block.
42
+ #
43
+ # The result is not guaranteed to be stable. When the comparison of two elements returns +0+, the order of the
44
+ # elements is unpredictable.
45
+ #
46
+ # If no block is given, an Enumerator is returned instead.
47
+ #
48
+ # @return [Array] if a block is given, the sorted array
49
+ # @return [Enumerator] if no block is given, an Enumerator
18
50
  def heap_sort_by(&block)
19
51
  if block_given?
20
52
  dup.heap_sort_by!(&block)
@@ -23,6 +55,16 @@ class Array
23
55
  end
24
56
  end
25
57
 
58
+ # Sorts +self+ in place with heap sort algorithm, using a set of keys generated by mapping the values in self through
59
+ # the given block.
60
+ #
61
+ # The result is not guaranteed to be stable. When the comparison of two elements returns +0+, the order of the
62
+ # elements is unpredictable.
63
+ #
64
+ # If no block is given, an Enumerator is returned instead.
65
+ #
66
+ # @return [Array] if a block is given, the sorted array
67
+ # @return [Enumerator] if no block is given, an Enumerator
26
68
  def heap_sort_by!(&_block)
27
69
  if block_given?
28
70
  heap_sort! do |a, b|
@@ -35,6 +77,7 @@ class Array
35
77
 
36
78
  private
37
79
 
80
+ # @private
38
81
  def heapify(&block)
39
82
  end_ = length - 1
40
83
  (length / 2 - 1).downto(0) do |start|
@@ -42,6 +85,7 @@ class Array
42
85
  end
43
86
  end
44
87
 
88
+ # @private
45
89
  def sift_down(start, end_, &block)
46
90
  root = start
47
91
  loop do
@@ -53,10 +97,12 @@ class Array
53
97
  end
54
98
  end
55
99
 
100
+ # @private
56
101
  def check_less_than_sibling(child, end_, &block)
57
102
  child + 1 <= end_ && sort_compare(self[child], self[child + 1], &block) == -1
58
103
  end
59
104
 
105
+ # @private
60
106
  def check_max_heap_order(root, child, &block)
61
107
  return true unless sort_compare(self[root], self[child], &block) == -1
62
108
  swap root, child
@@ -6,6 +6,7 @@ class Array
6
6
  # @param index1 [Integer] The index of the first element.
7
7
  # @param index2 [Integer] The index of the second element.
8
8
  # @return [Array]
9
+ # @raise [ArgumentError] If either of the two parameters is not an Integer.
9
10
  def swap(index1, index2)
10
11
  raise ArgumentError, 'Index must be an integer.' unless index1.is_a?(Integer) && index2.is_a?(Integer)
11
12
  self[index1], self[index2] = self[index2], self[index1] if index1 != index2
@@ -16,7 +17,7 @@ class Array
16
17
 
17
18
  # Compare two elements.
18
19
  #
19
- # If a block is provided, it will be used to compare the two elements. If not, +<=>+ will be used.
20
+ # If a block is provided, it will be used to compare the two elements. If not, operator +<=>+ will be used.
20
21
  def sort_compare(element1, element2, &_block)
21
22
  if block_given?
22
23
  yield element1, element2
@@ -1,10 +1,32 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Array
4
+ # Returns a new array created by sorting +self+, using insertion sort algorithm.
5
+ #
6
+ # Comparisons for the sort will be done using the <=> operator or using an optional code block.
7
+ #
8
+ # The block must implement a comparison between +a+ and +b+ and return an integer less than 0 when +b+ follows +a+,
9
+ # +0+ when +a+ and +b+ are equivalent, or an integer greater than 0 when +a+ follows +b+.
10
+ #
11
+ # The result is guaranteed to be stable. When the comparison of two elements returns +0+, the order of the elements
12
+ # will be preserved.
13
+ #
14
+ # @return [Array] the sorted array
4
15
  def insertion_sort(&block)
5
16
  dup.insertion_sort!(&block)
6
17
  end
7
18
 
19
+ # Sorts +self+ in place, using insertion sort algorithm.
20
+ #
21
+ # Comparisons for the sort will be done using the <=> operator or using an optional code block.
22
+ #
23
+ # The block must implement a comparison between +a+ and +b+ and return an integer less than 0 when +b+ follows +a+,
24
+ # +0+ when +a+ and +b+ are equivalent, or an integer greater than 0 when +a+ follows +b+.
25
+ #
26
+ # The result is guaranteed to be stable. When the comparison of two elements returns +0+, the order of the elements
27
+ # will be preserved.
28
+ #
29
+ # @return [Array] +self+
8
30
  def insertion_sort!(&block)
9
31
  return self if length <= 1
10
32
  (1...length).each do |i|
@@ -16,6 +38,16 @@ class Array
16
38
  self
17
39
  end
18
40
 
41
+ # Returns a new array created by sorting +self+ with insertion sort algorithm, using a set of keys generated by
42
+ # mapping the values in self through the given block.
43
+ #
44
+ # The result is guaranteed to be stable. When the comparison of two elements returns +0+, the order of the elements
45
+ # will be preserved.
46
+ #
47
+ # If no block is given, an Enumerator is returned instead.
48
+ #
49
+ # @return [Array] if a block is given, the sorted array
50
+ # @return [Enumerator] if no block is given, an Enumerator
19
51
  def insertion_sort_by(&block)
20
52
  if block_given?
21
53
  dup.insertion_sort_by!(&block)
@@ -24,6 +56,16 @@ class Array
24
56
  end
25
57
  end
26
58
 
59
+ # Sorts +self+ in place with insertion sort algorithm, using a set of keys generated by mapping the values in self
60
+ # through the given block.
61
+ #
62
+ # The result is guaranteed to be stable. When the comparison of two elements returns +0+, the order of the elements
63
+ # will be preserved.
64
+ #
65
+ # If no block is given, an Enumerator is returned instead.
66
+ #
67
+ # @return [Array] if a block is given, the sorted array
68
+ # @return [Enumerator] if no block is given, an Enumerator
27
69
  def insertion_sort_by!(&_block)
28
70
  if block_given?
29
71
  insertion_sort! do |a, b|
@@ -1,6 +1,17 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Array
4
+ # Returns a new array created by sorting +self+, using merge sort algorithm.
5
+ #
6
+ # Comparisons for the sort will be done using the <=> operator or using an optional code block.
7
+ #
8
+ # The block must implement a comparison between +a+ and +b+ and return an integer less than 0 when +b+ follows +a+,
9
+ # +0+ when +a+ and +b+ are equivalent, or an integer greater than 0 when +a+ follows +b+.
10
+ #
11
+ # The result is guaranteed to be stable. When the comparison of two elements returns +0+, the order of the elements
12
+ # will be preserved.
13
+ #
14
+ # @return [Array] the sorted array
4
15
  def merge_sort(&block)
5
16
  return dup if length <= 1
6
17
  divided_parts = merge_sort_divide
@@ -9,10 +20,31 @@ class Array
9
20
  merge_sort_merge part_a_sorted, part_b_sorted, &block
10
21
  end
11
22
 
23
+ # Sorts +self+ in place, using merge sort algorithm.
24
+ #
25
+ # Comparisons for the sort will be done using the <=> operator or using an optional code block.
26
+ #
27
+ # The block must implement a comparison between +a+ and +b+ and return an integer less than 0 when +b+ follows +a+,
28
+ # +0+ when +a+ and +b+ are equivalent, or an integer greater than 0 when +a+ follows +b+.
29
+ #
30
+ # The result is guaranteed to be stable. When the comparison of two elements returns +0+, the order of the elements
31
+ # will be preserved.
32
+ #
33
+ # @return [Array] +self+
12
34
  def merge_sort!(&block)
13
35
  become_clone_of merge_sort(&block)
14
36
  end
15
37
 
38
+ # Returns a new array created by sorting +self+ with merge sort algorithm, using a set of keys generated by mapping
39
+ # the values in self through the given block.
40
+ #
41
+ # The result is guaranteed to be stable. When the comparison of two elements returns +0+, the order of the elements
42
+ # will be preserved.
43
+ #
44
+ # If no block is given, an Enumerator is returned instead.
45
+ #
46
+ # @return [Array] if a block is given, the sorted array
47
+ # @return [Enumerator] if no block is given, an Enumerator
16
48
  def merge_sort_by(&_block)
17
49
  if block_given?
18
50
  merge_sort do |a, b|
@@ -23,6 +55,16 @@ class Array
23
55
  end
24
56
  end
25
57
 
58
+ # Sorts +self+ in place with merge sort algorithm, using a set of keys generated by mapping the values in self through
59
+ # the given block.
60
+ #
61
+ # The result is guaranteed to be stable. When the comparison of two elements returns +0+, the order of the elements
62
+ # will be preserved.
63
+ #
64
+ # If no block is given, an Enumerator is returned instead.
65
+ #
66
+ # @return [Array] if a block is given, the sorted array
67
+ # @return [Enumerator] if no block is given, an Enumerator
26
68
  def merge_sort_by!(&block)
27
69
  if block_given?
28
70
  become_clone_of merge_sort_by(&block)
@@ -33,6 +75,7 @@ class Array
33
75
 
34
76
  private
35
77
 
78
+ # @private
36
79
  def merge_sort_divide
37
80
  mid = length / 2
38
81
  part_a = self[0, mid]
@@ -40,6 +83,7 @@ class Array
40
83
  [part_a, part_b]
41
84
  end
42
85
 
86
+ # @private
43
87
  def merge_sort_merge(part_a, part_b, &block)
44
88
  result = []
45
89
  while part_a.length.positive? && part_b.length.positive?
@@ -1,16 +1,48 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Array
4
+ # Returns a new array created by sorting +self+, using quicksort algorithm.
5
+ #
6
+ # Comparisons for the sort will be done using the <=> operator or using an optional code block.
7
+ #
8
+ # The block must implement a comparison between +a+ and +b+ and return an integer less than 0 when +b+ follows +a+,
9
+ # +0+ when +a+ and +b+ are equivalent, or an integer greater than 0 when +a+ follows +b+.
10
+ #
11
+ # The result is not guaranteed to be stable. When the comparison of two elements returns +0+, the order of the
12
+ # elements is unpredictable.
13
+ #
14
+ # @return [Array] the sorted array
4
15
  def quick_sort(&block)
5
16
  return dup if length <= 1
6
17
  left, right = self[1..-1].partition { |y| sort_compare(first, y, &block).positive? }
7
18
  left.quick_sort + [first] + right.quick_sort
8
19
  end
9
20
 
21
+ # Sorts +self+ in place, using quicksort algorithm.
22
+ #
23
+ # Comparisons for the sort will be done using the <=> operator or using an optional code block.
24
+ #
25
+ # The block must implement a comparison between +a+ and +b+ and return an integer less than 0 when +b+ follows +a+,
26
+ # +0+ when +a+ and +b+ are equivalent, or an integer greater than 0 when +a+ follows +b+.
27
+ #
28
+ # The result is not guaranteed to be stable. When the comparison of two elements returns +0+, the order of the
29
+ # elements is unpredictable.
30
+ #
31
+ # @return [Array] +self+
10
32
  def quick_sort!(&block)
11
33
  become_clone_of quick_sort(&block)
12
34
  end
13
35
 
36
+ # Returns a new array created by sorting +self+ with quicksort algorithm, using a set of keys generated by mapping
37
+ # the values in self through the given block.
38
+ #
39
+ # The result is not guaranteed to be stable. When the comparison of two elements returns +0+, the order of the
40
+ # elements is unpredictable.
41
+ #
42
+ # If no block is given, an Enumerator is returned instead.
43
+ #
44
+ # @return [Array] if a block is given, the sorted array
45
+ # @return [Enumerator] if no block is given, an Enumerator
14
46
  def quick_sort_by(&_block)
15
47
  if block_given?
16
48
  quick_sort do |a, b|
@@ -21,6 +53,16 @@ class Array
21
53
  end
22
54
  end
23
55
 
56
+ # Sorts +self+ in place with quicksort algorithm, using a set of keys generated by mapping the values in self through
57
+ # the given block.
58
+ #
59
+ # The result is not guaranteed to be stable. When the comparison of two elements returns +0+, the order of the
60
+ # elements is unpredictable.
61
+ #
62
+ # If no block is given, an Enumerator is returned instead.
63
+ #
64
+ # @return [Array] if a block is given, the sorted array
65
+ # @return [Enumerator] if no block is given, an Enumerator
24
66
  def quick_sort_by!(&block)
25
67
  if block_given?
26
68
  become_clone_of quick_sort_by(&block)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ArraySort
4
- VERSION = '0.1.0'
4
+ VERSION = '0.1.1'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: array-sort
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Leo Liang
@@ -52,8 +52,8 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '10.0'
55
- description: Implements sorting algorithms such as merge sort, bubble sort, insertion
56
- sort, etc. for Arrays.
55
+ description: Implements sorting algorithms such as merge sort, bubble sort, heap sort,
56
+ etc. for Arrays.
57
57
  email:
58
58
  - developer@leoliang.com
59
59
  executables: []
@@ -102,5 +102,5 @@ rubyforge_project:
102
102
  rubygems_version: 2.7.6
103
103
  signing_key:
104
104
  specification_version: 4
105
- summary: Implementations of different sorting algorithms for Arrays.
105
+ summary: Implementations of popular sorting algorithms for Arrays.
106
106
  test_files: []