array-sort 0.1.1 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +73 -0
  3. data/README.md +228 -29
  4. data/lib/array/sort.rb +5 -8
  5. data/lib/array_sort/algorithms/binary_insertion_sort.rb +41 -0
  6. data/lib/array_sort/algorithms/bubble_sort.rb +31 -0
  7. data/lib/array_sort/algorithms/bucket_sort.rb +67 -0
  8. data/lib/array_sort/algorithms/cocktail_shaker_sort.rb +42 -0
  9. data/lib/array_sort/algorithms/comb_sort.rb +33 -0
  10. data/lib/array_sort/algorithms/counting_sort.rb +50 -0
  11. data/lib/array_sort/algorithms/cycle_sort.rb +46 -0
  12. data/lib/array_sort/algorithms/gnome_sort.rb +28 -0
  13. data/lib/array_sort/algorithms/heap_sort.rb +43 -0
  14. data/lib/array_sort/algorithms/insertion_sort.rb +35 -0
  15. data/lib/array_sort/algorithms/intro_sort.rb +80 -0
  16. data/lib/array_sort/algorithms/merge_sort.rb +57 -0
  17. data/lib/array_sort/algorithms/odd_even_sort.rb +34 -0
  18. data/lib/array_sort/algorithms/pancake_sort.rb +39 -0
  19. data/lib/array_sort/algorithms/quick_sort.rb +68 -0
  20. data/lib/array_sort/algorithms/radix_sort.rb +58 -0
  21. data/lib/array_sort/algorithms/selection_sort.rb +25 -0
  22. data/lib/array_sort/algorithms/shell_sort.rb +41 -0
  23. data/lib/array_sort/algorithms/smooth_sort.rb +106 -0
  24. data/lib/array_sort/algorithms/tim_sort.rb +335 -0
  25. data/lib/array_sort/algorithms/tournament_sort.rb +53 -0
  26. data/lib/array_sort/algorithms/tree_sort.rb +96 -0
  27. data/lib/array_sort/array_methods.rb +67 -0
  28. data/lib/array_sort/core.rb +124 -0
  29. data/lib/array_sort/functions.rb +23 -0
  30. data/lib/array_sort/refinements.rb +16 -0
  31. data/lib/array_sort/registry.rb +68 -0
  32. data/lib/array_sort/trace.rb +111 -0
  33. data/lib/{array/sort → array_sort}/version.rb +1 -1
  34. data/lib/array_sort.rb +43 -0
  35. data/sig/array_sort.rbs +368 -0
  36. metadata +47 -71
  37. data/.gitignore +0 -9
  38. data/.rubocop.yml +0 -11
  39. data/.travis.yml +0 -6
  40. data/Gemfile +0 -6
  41. data/Gemfile.lock +0 -22
  42. data/Rakefile +0 -10
  43. data/array-sort.gemspec +0 -37
  44. data/bin/console +0 -14
  45. data/bin/setup +0 -8
  46. data/lib/array/sort/bubble_sort.rb +0 -91
  47. data/lib/array/sort/heap_sort.rb +0 -111
  48. data/lib/array/sort/helper.rb +0 -34
  49. data/lib/array/sort/insertion_sort.rb +0 -78
  50. data/lib/array/sort/merge_sort.rb +0 -94
  51. data/lib/array/sort/quick_sort.rb +0 -73
metadata CHANGED
@@ -1,89 +1,67 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: array-sort
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Leo Liang
8
- autorequire:
9
- bindir: exe
8
+ bindir: bin
10
9
  cert_chain: []
11
- date: 2018-05-11 00:00:00.000000000 Z
12
- dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: bundler
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '1.16'
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: '1.16'
27
- - !ruby/object:Gem::Dependency
28
- name: minitest
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: '5.0'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: '5.0'
41
- - !ruby/object:Gem::Dependency
42
- name: rake
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - "~>"
46
- - !ruby/object:Gem::Version
47
- version: '10.0'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - "~>"
53
- - !ruby/object:Gem::Version
54
- version: '10.0'
55
- description: Implements sorting algorithms such as merge sort, bubble sort, heap sort,
56
- etc. for Arrays.
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: '22 sorting algorithms (merge sort, quicksort, Timsort, introsort, radix
13
+ sort, smoothsort and more) with the same interface as Array#sort, #sort!, #sort_by
14
+ and #sort_by!, available as Array methods, refinements or module functions, plus
15
+ a tracer to watch them work.'
57
16
  email:
58
17
  - developer@leoliang.com
59
18
  executables: []
60
19
  extensions: []
61
20
  extra_rdoc_files: []
62
21
  files:
63
- - ".gitignore"
64
- - ".rubocop.yml"
65
- - ".travis.yml"
66
- - Gemfile
67
- - Gemfile.lock
22
+ - CHANGELOG.md
68
23
  - LICENSE.txt
69
24
  - README.md
70
- - Rakefile
71
- - array-sort.gemspec
72
- - bin/console
73
- - bin/setup
74
25
  - lib/array/sort.rb
75
- - lib/array/sort/bubble_sort.rb
76
- - lib/array/sort/heap_sort.rb
77
- - lib/array/sort/helper.rb
78
- - lib/array/sort/insertion_sort.rb
79
- - lib/array/sort/merge_sort.rb
80
- - lib/array/sort/quick_sort.rb
81
- - lib/array/sort/version.rb
26
+ - lib/array_sort.rb
27
+ - lib/array_sort/algorithms/binary_insertion_sort.rb
28
+ - lib/array_sort/algorithms/bubble_sort.rb
29
+ - lib/array_sort/algorithms/bucket_sort.rb
30
+ - lib/array_sort/algorithms/cocktail_shaker_sort.rb
31
+ - lib/array_sort/algorithms/comb_sort.rb
32
+ - lib/array_sort/algorithms/counting_sort.rb
33
+ - lib/array_sort/algorithms/cycle_sort.rb
34
+ - lib/array_sort/algorithms/gnome_sort.rb
35
+ - lib/array_sort/algorithms/heap_sort.rb
36
+ - lib/array_sort/algorithms/insertion_sort.rb
37
+ - lib/array_sort/algorithms/intro_sort.rb
38
+ - lib/array_sort/algorithms/merge_sort.rb
39
+ - lib/array_sort/algorithms/odd_even_sort.rb
40
+ - lib/array_sort/algorithms/pancake_sort.rb
41
+ - lib/array_sort/algorithms/quick_sort.rb
42
+ - lib/array_sort/algorithms/radix_sort.rb
43
+ - lib/array_sort/algorithms/selection_sort.rb
44
+ - lib/array_sort/algorithms/shell_sort.rb
45
+ - lib/array_sort/algorithms/smooth_sort.rb
46
+ - lib/array_sort/algorithms/tim_sort.rb
47
+ - lib/array_sort/algorithms/tournament_sort.rb
48
+ - lib/array_sort/algorithms/tree_sort.rb
49
+ - lib/array_sort/array_methods.rb
50
+ - lib/array_sort/core.rb
51
+ - lib/array_sort/functions.rb
52
+ - lib/array_sort/refinements.rb
53
+ - lib/array_sort/registry.rb
54
+ - lib/array_sort/trace.rb
55
+ - lib/array_sort/version.rb
56
+ - sig/array_sort.rbs
82
57
  homepage: https://github.com/foxhatleo/array-sort
83
58
  licenses:
84
59
  - MIT
85
- metadata: {}
86
- post_install_message:
60
+ metadata:
61
+ source_code_uri: https://github.com/foxhatleo/array-sort
62
+ changelog_uri: https://github.com/foxhatleo/array-sort/blob/master/CHANGELOG.md
63
+ bug_tracker_uri: https://github.com/foxhatleo/array-sort/issues
64
+ rubygems_mfa_required: 'true'
87
65
  rdoc_options: []
88
66
  require_paths:
89
67
  - lib
@@ -91,16 +69,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
91
69
  requirements:
92
70
  - - ">="
93
71
  - !ruby/object:Gem::Version
94
- version: '0'
72
+ version: '3.3'
95
73
  required_rubygems_version: !ruby/object:Gem::Requirement
96
74
  requirements:
97
75
  - - ">="
98
76
  - !ruby/object:Gem::Version
99
77
  version: '0'
100
78
  requirements: []
101
- rubyforge_project:
102
- rubygems_version: 2.7.6
103
- signing_key:
79
+ rubygems_version: 4.0.20
104
80
  specification_version: 4
105
81
  summary: Implementations of popular sorting algorithms for Arrays.
106
82
  test_files: []
data/.gitignore DELETED
@@ -1,9 +0,0 @@
1
- /.bundle/
2
- /.yardoc
3
- /_yardoc/
4
- /coverage/
5
- /doc/
6
- /pkg/
7
- /spec/reports/
8
- /tmp/
9
- /.idea/
data/.rubocop.yml DELETED
@@ -1,11 +0,0 @@
1
- Style/Documentation:
2
- Enabled: false
3
-
4
- #Naming/FileName:
5
- # Enabled: true
6
-
7
- Metrics/LineLength:
8
- Max: 120
9
-
10
- Layout/TrailingWhitespace:
11
- Enabled: false
data/.travis.yml DELETED
@@ -1,6 +0,0 @@
1
- sudo: false
2
- language: ruby
3
- cache: bundler
4
- rvm:
5
- - 2.5.1
6
- before_install: gem install bundler -v 1.16.1
data/Gemfile DELETED
@@ -1,6 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
-
5
- # Specify your gem's dependencies in array-sort.gemspec
6
- gemspec
data/Gemfile.lock DELETED
@@ -1,22 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- array-sort (0.1.1)
5
-
6
- GEM
7
- remote: https://rubygems.org/
8
- specs:
9
- minitest (5.11.3)
10
- rake (10.5.0)
11
-
12
- PLATFORMS
13
- ruby
14
-
15
- DEPENDENCIES
16
- array-sort!
17
- bundler (~> 1.16)
18
- minitest (~> 5.0)
19
- rake (~> 10.0)
20
-
21
- BUNDLED WITH
22
- 1.16.1
data/Rakefile DELETED
@@ -1,10 +0,0 @@
1
- require "bundler/gem_tasks"
2
- require "rake/testtask"
3
-
4
- Rake::TestTask.new(:test) do |t|
5
- t.libs << "test"
6
- t.libs << "lib"
7
- t.test_files = FileList["test/**/*_test.rb"]
8
- end
9
-
10
- task :default => :test
data/array-sort.gemspec DELETED
@@ -1,37 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- lib = File.expand_path('lib', __dir__)
4
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
- require 'array/sort/version'
6
-
7
- Gem::Specification.new do |spec|
8
- spec.name = 'array-sort'
9
- spec.version = ArraySort::VERSION
10
- spec.authors = ['Leo Liang']
11
- spec.email = ['developer@leoliang.com']
12
-
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
- spec.homepage = 'https://github.com/foxhatleo/array-sort'
16
- spec.license = 'MIT'
17
-
18
- # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
19
- # to allow pushing to a single host or delete this section to allow pushing to any host.
20
- # if spec.respond_to?(:metadata)
21
- # spec.metadata['allowed_push_host'] = 'TODO: Set to \'http://mygemserver.com\''
22
- # else
23
- # raise 'RubyGems 2.0 or newer is required to protect against ' \
24
- # 'public gem pushes.'
25
- # end
26
-
27
- spec.files = `git ls-files -z`.split("\x0").reject do |f|
28
- f.match(%r{^(test|spec|features)/})
29
- end
30
- spec.bindir = 'exe'
31
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
32
- spec.require_paths = ['lib']
33
-
34
- spec.add_development_dependency 'bundler', '~> 1.16'
35
- spec.add_development_dependency 'minitest', '~> 5.0'
36
- spec.add_development_dependency 'rake', '~> 10.0'
37
- end
data/bin/console DELETED
@@ -1,14 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require "bundler/setup"
4
- require "array/sort"
5
-
6
- # You can add fixtures and/or initialization code here to make experimenting
7
- # with your gem easier. You can also use a different console, if you like.
8
-
9
- # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
11
- # Pry.start
12
-
13
- require "irb"
14
- IRB.start(__FILE__)
data/bin/setup DELETED
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here
@@ -1,91 +0,0 @@
1
- # frozen_string_literal: true
2
-
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
15
- def bubble_sort(&block)
16
- dup.bubble_sort!(&block)
17
- end
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+
30
- def bubble_sort!(&block)
31
- return self if length <= 1
32
- n = length
33
- loop do
34
- new_n = bubble_sort_check(n, &block)
35
- n = new_n
36
- break if n.zero?
37
- end
38
- self
39
- end
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
51
- def bubble_sort_by(&block)
52
- if block_given?
53
- dup.bubble_sort_by!(&block)
54
- else
55
- to_enum :bubble_sort_by
56
- end
57
- end
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
69
- def bubble_sort_by!(&_block)
70
- if block_given?
71
- bubble_sort! do |a, b|
72
- yield(a) <=> yield(b)
73
- end
74
- else
75
- to_enum :bubble_sort_by!
76
- end
77
- end
78
-
79
- private
80
-
81
- # @private
82
- def bubble_sort_check(length_checking, &block)
83
- new_n = 0
84
- (1...length_checking).each do |i|
85
- next unless sort_compare(self[i - 1], self[i], &block).positive?
86
- swap i - 1, i
87
- new_n = i
88
- end
89
- new_n
90
- end
91
- end
@@ -1,111 +0,0 @@
1
- # frozen_string_literal: true
2
-
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
15
- def heap_sort(&block)
16
- dup.heap_sort!(&block)
17
- end
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+
30
- def heap_sort!(&block)
31
- return self if length <= 1
32
- heapify(&block)
33
- (length - 1).downto(1) do |end_|
34
- swap(end_, 0)
35
- sift_down(0, end_ - 1, &block)
36
- end
37
- self
38
- end
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
50
- def heap_sort_by(&block)
51
- if block_given?
52
- dup.heap_sort_by!(&block)
53
- else
54
- to_enum :heap_sort_by
55
- end
56
- end
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
68
- def heap_sort_by!(&_block)
69
- if block_given?
70
- heap_sort! do |a, b|
71
- yield(a) <=> yield(b)
72
- end
73
- else
74
- to_enum :heap_sort_by!
75
- end
76
- end
77
-
78
- private
79
-
80
- # @private
81
- def heapify(&block)
82
- end_ = length - 1
83
- (length / 2 - 1).downto(0) do |start|
84
- sift_down(start, end_, &block)
85
- end
86
- end
87
-
88
- # @private
89
- def sift_down(start, end_, &block)
90
- root = start
91
- loop do
92
- child = root * 2 + 1
93
- break if child > end_
94
- child += 1 if check_less_than_sibling(child, end_, &block)
95
- break if check_max_heap_order(root, child, &block)
96
- root = child
97
- end
98
- end
99
-
100
- # @private
101
- def check_less_than_sibling(child, end_, &block)
102
- child + 1 <= end_ && sort_compare(self[child], self[child + 1], &block) == -1
103
- end
104
-
105
- # @private
106
- def check_max_heap_order(root, child, &block)
107
- return true unless sort_compare(self[root], self[child], &block) == -1
108
- swap root, child
109
- false
110
- end
111
- end
@@ -1,34 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Array
4
- # Swap two elements in the array, and returns +self+.
5
- #
6
- # @param index1 [Integer] The index of the first element.
7
- # @param index2 [Integer] The index of the second element.
8
- # @return [Array]
9
- # @raise [ArgumentError] If either of the two parameters is not an Integer.
10
- def swap(index1, index2)
11
- raise ArgumentError, 'Index must be an integer.' unless index1.is_a?(Integer) && index2.is_a?(Integer)
12
- self[index1], self[index2] = self[index2], self[index1] if index1 != index2
13
- self
14
- end
15
-
16
- private
17
-
18
- # Compare two elements.
19
- #
20
- # If a block is provided, it will be used to compare the two elements. If not, operator +<=>+ will be used.
21
- def sort_compare(element1, element2, &_block)
22
- if block_given?
23
- yield element1, element2
24
- else
25
- element1 <=> element2
26
- end
27
- end
28
-
29
- # Clear everything in +self+, and add all elements in +target+.
30
- def become_clone_of(target)
31
- clear
32
- concat target
33
- end
34
- end
@@ -1,78 +0,0 @@
1
- # frozen_string_literal: true
2
-
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
15
- def insertion_sort(&block)
16
- dup.insertion_sort!(&block)
17
- end
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+
30
- def insertion_sort!(&block)
31
- return self if length <= 1
32
- (1...length).each do |i|
33
- i.downto(1) do |j|
34
- break unless sort_compare(self[j - 1], self[j], &block) == 1
35
- swap(j - 1, j)
36
- end
37
- end
38
- self
39
- end
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
51
- def insertion_sort_by(&block)
52
- if block_given?
53
- dup.insertion_sort_by!(&block)
54
- else
55
- to_enum :insertion_sort_by
56
- end
57
- end
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
69
- def insertion_sort_by!(&_block)
70
- if block_given?
71
- insertion_sort! do |a, b|
72
- yield(a) <=> yield(b)
73
- end
74
- else
75
- to_enum :insertion_sort_by!
76
- end
77
- end
78
- end