array-sort 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 19712ae85993b11b3c6aa80b1b12557b3fe44081c736588fdb95491c888f9465
4
+ data.tar.gz: 52adc3ab55c79a36af340b7ba0276a3ea31a759f6e93224dc3496d8c13d34d4e
5
+ SHA512:
6
+ metadata.gz: 2f001fcef739c8843ea6b6d66813123d5c51c8c8ba2bc9d7ba633859914270671701087cc93a85ec7a7591582eab0d7e757c1be3a1d3a77fc14ea183dd56174e
7
+ data.tar.gz: 64cafd15ee8abe2385debadd6f95659e2a7b35a8f7dfd9c61799ba1532baa62f24775089d505937ef36e4277be1c404135cee88cb4d55cef2624c003e9a307fc
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /.idea/
data/.rubocop.yml ADDED
@@ -0,0 +1,11 @@
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 ADDED
@@ -0,0 +1,6 @@
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 ADDED
@@ -0,0 +1,6 @@
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 ADDED
@@ -0,0 +1,22 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ array-sort (0.1.0)
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/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 TODO: Write your name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,69 @@
1
+ # ArraySort
2
+
3
+ ArraySort adds methods in the Ruby `Array` class that provides sorting methods using a few sorting algorithms.
4
+
5
+ Currently, the following sorting algorithms are implemented:
6
+ * Bubble sort _(stable)_
7
+ * Heap sort _(unstable)_
8
+ * Insertion sort _(stable)_
9
+ * Merge sort _(stable)_
10
+ * Quicksort _(unstable)_
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.
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ ```ruby
20
+ gem 'array-sort'
21
+ ```
22
+
23
+ And then execute:
24
+
25
+ $ bundle
26
+
27
+ Or install it yourself as:
28
+
29
+ $ gem install array-sort
30
+
31
+ ## Usage
32
+
33
+ All sort methods share the same signature of `Array#sort`, `Array#sort!`, `Array#sort_by` and `Array#sort_by!`. Simply
34
+ change the names of the sorting methods to the following corresponding methods of a specific sorting algorithm:
35
+
36
+ * Bubble sort: `bubble_sort`, `bubble_sort!`, `bubble_sort_by`, `bubble_sort_by!`
37
+ * Heap sort: `heap_sort`, `heap_sort!`, `heap_sort_by`, `heap_sort_by!`
38
+ * Insertion sort: `insertion_sort`, `insertion_sort!`, `insertion_sort_by`, `insertion_sort_by!`
39
+ * Merge sort: `merge_sort`, `merge_sort!`, `merge_sort_by`, `merge_sort_by!`
40
+ * Quicksort: `quick_sort`, `quick_sort!`, `quick_sort_by`, `quick_sort_by!`
41
+
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`.
44
+
45
+ For example, to sort an array using insertion sort:
46
+
47
+ ```ruby
48
+ [3, 1, 2].insertion_sort # => [1, 2, 3]
49
+
50
+ # The following two lines both produce [[1, 4], [2, 6], [3, 3]]
51
+ [[3, 3], [1, 4], [2, 6]].insertion_sort { |a, b| a[0] <=> b[0] }
52
+ [[3, 3], [1, 4], [2, 6]].insertion_sort_by { |e| e[0] }
53
+ ```
54
+
55
+ ## Development
56
+
57
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can
58
+ also run `bin/console` for an interactive prompt that will allow you to experiment.
59
+
60
+ ## Contributing
61
+
62
+ Bug reports and pull requests are welcome on GitHub at
63
+ [https://github.com/foxhatleo/array-sort](https://github.com/foxhatleo/array-sort). This project is intended to be a
64
+ safe, welcoming space for collaboration, and contributors are expected to adhere to the
65
+ [Contributor Covenant](http://contributor-covenant.org) code of conduct.
66
+
67
+ ## License
68
+
69
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
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
@@ -0,0 +1,37 @@
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 different sorting algorithms for Arrays.'
14
+ spec.description = 'Implements sorting algorithms such as merge sort, bubble sort, insertion 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 ADDED
@@ -0,0 +1,14 @@
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 ADDED
@@ -0,0 +1,8 @@
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
data/lib/array/sort.rb ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'array/sort/version'
4
+ require 'array/sort/helper'
5
+ require 'array/sort/merge_sort'
6
+ require 'array/sort/insertion_sort'
7
+ require 'array/sort/heap_sort'
8
+ require 'array/sort/bubble_sort'
9
+ require 'array/sort/quick_sort'
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Array
4
+ def bubble_sort(&block)
5
+ dup.bubble_sort!(&block)
6
+ end
7
+
8
+ def bubble_sort!(&block)
9
+ return self if length <= 1
10
+ n = length
11
+ loop do
12
+ new_n = bubble_sort_check(n, &block)
13
+ n = new_n
14
+ break if n.zero?
15
+ end
16
+ self
17
+ end
18
+
19
+ def bubble_sort_by(&block)
20
+ if block_given?
21
+ dup.bubble_sort_by!(&block)
22
+ else
23
+ to_enum :bubble_sort_by
24
+ end
25
+ end
26
+
27
+ def bubble_sort_by!(&_block)
28
+ if block_given?
29
+ bubble_sort! do |a, b|
30
+ yield(a) <=> yield(b)
31
+ end
32
+ else
33
+ to_enum :bubble_sort_by!
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ def bubble_sort_check(length_checking, &block)
40
+ new_n = 0
41
+ (1...length_checking).each do |i|
42
+ next unless sort_compare(self[i - 1], self[i], &block).positive?
43
+ swap i - 1, i
44
+ new_n = i
45
+ end
46
+ new_n
47
+ end
48
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Array
4
+ def heap_sort(&block)
5
+ dup.heap_sort!(&block)
6
+ end
7
+
8
+ def heap_sort!(&block)
9
+ return self if length <= 1
10
+ heapify(&block)
11
+ (length - 1).downto(1) do |end_|
12
+ swap(end_, 0)
13
+ sift_down(0, end_ - 1, &block)
14
+ end
15
+ self
16
+ end
17
+
18
+ def heap_sort_by(&block)
19
+ if block_given?
20
+ dup.heap_sort_by!(&block)
21
+ else
22
+ to_enum :heap_sort_by
23
+ end
24
+ end
25
+
26
+ def heap_sort_by!(&_block)
27
+ if block_given?
28
+ heap_sort! do |a, b|
29
+ yield(a) <=> yield(b)
30
+ end
31
+ else
32
+ to_enum :heap_sort_by!
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ def heapify(&block)
39
+ end_ = length - 1
40
+ (length / 2 - 1).downto(0) do |start|
41
+ sift_down(start, end_, &block)
42
+ end
43
+ end
44
+
45
+ def sift_down(start, end_, &block)
46
+ root = start
47
+ loop do
48
+ child = root * 2 + 1
49
+ break if child > end_
50
+ child += 1 if check_less_than_sibling(child, end_, &block)
51
+ break if check_max_heap_order(root, child, &block)
52
+ root = child
53
+ end
54
+ end
55
+
56
+ def check_less_than_sibling(child, end_, &block)
57
+ child + 1 <= end_ && sort_compare(self[child], self[child + 1], &block) == -1
58
+ end
59
+
60
+ def check_max_heap_order(root, child, &block)
61
+ return true unless sort_compare(self[root], self[child], &block) == -1
62
+ swap root, child
63
+ false
64
+ end
65
+ end
@@ -0,0 +1,33 @@
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
+ def swap(index1, index2)
10
+ raise ArgumentError, 'Index must be an integer.' unless index1.is_a?(Integer) && index2.is_a?(Integer)
11
+ self[index1], self[index2] = self[index2], self[index1] if index1 != index2
12
+ self
13
+ end
14
+
15
+ private
16
+
17
+ # Compare two elements.
18
+ #
19
+ # If a block is provided, it will be used to compare the two elements. If not, +<=>+ will be used.
20
+ def sort_compare(element1, element2, &_block)
21
+ if block_given?
22
+ yield element1, element2
23
+ else
24
+ element1 <=> element2
25
+ end
26
+ end
27
+
28
+ # Clear everything in +self+, and add all elements in +target+.
29
+ def become_clone_of(target)
30
+ clear
31
+ concat target
32
+ end
33
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Array
4
+ def insertion_sort(&block)
5
+ dup.insertion_sort!(&block)
6
+ end
7
+
8
+ def insertion_sort!(&block)
9
+ return self if length <= 1
10
+ (1...length).each do |i|
11
+ i.downto(1) do |j|
12
+ break unless sort_compare(self[j - 1], self[j], &block) == 1
13
+ swap(j - 1, j)
14
+ end
15
+ end
16
+ self
17
+ end
18
+
19
+ def insertion_sort_by(&block)
20
+ if block_given?
21
+ dup.insertion_sort_by!(&block)
22
+ else
23
+ to_enum :insertion_sort_by
24
+ end
25
+ end
26
+
27
+ def insertion_sort_by!(&_block)
28
+ if block_given?
29
+ insertion_sort! do |a, b|
30
+ yield(a) <=> yield(b)
31
+ end
32
+ else
33
+ to_enum :insertion_sort_by!
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Array
4
+ def merge_sort(&block)
5
+ return dup if length <= 1
6
+ divided_parts = merge_sort_divide
7
+ part_a_sorted = divided_parts[0].merge_sort
8
+ part_b_sorted = divided_parts[1].merge_sort
9
+ merge_sort_merge part_a_sorted, part_b_sorted, &block
10
+ end
11
+
12
+ def merge_sort!(&block)
13
+ become_clone_of merge_sort(&block)
14
+ end
15
+
16
+ def merge_sort_by(&_block)
17
+ if block_given?
18
+ merge_sort do |a, b|
19
+ yield(a) <=> yield(b)
20
+ end
21
+ else
22
+ to_enum :merge_sort_by
23
+ end
24
+ end
25
+
26
+ def merge_sort_by!(&block)
27
+ if block_given?
28
+ become_clone_of merge_sort_by(&block)
29
+ else
30
+ to_enum :merge_sort_by!
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ def merge_sort_divide
37
+ mid = length / 2
38
+ part_a = self[0, mid]
39
+ part_b = self[mid..-1]
40
+ [part_a, part_b]
41
+ end
42
+
43
+ def merge_sort_merge(part_a, part_b, &block)
44
+ result = []
45
+ while part_a.length.positive? && part_b.length.positive?
46
+ result << (sort_compare(part_a.first, part_b.first, &block).positive? ? part_b : part_a).shift
47
+ end
48
+ result + part_a + part_b
49
+ end
50
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Array
4
+ def quick_sort(&block)
5
+ return dup if length <= 1
6
+ left, right = self[1..-1].partition { |y| sort_compare(first, y, &block).positive? }
7
+ left.quick_sort + [first] + right.quick_sort
8
+ end
9
+
10
+ def quick_sort!(&block)
11
+ become_clone_of quick_sort(&block)
12
+ end
13
+
14
+ def quick_sort_by(&_block)
15
+ if block_given?
16
+ quick_sort do |a, b|
17
+ yield(a) <=> yield(b)
18
+ end
19
+ else
20
+ to_enum :quick_sort_by
21
+ end
22
+ end
23
+
24
+ def quick_sort_by!(&block)
25
+ if block_given?
26
+ become_clone_of quick_sort_by(&block)
27
+ else
28
+ to_enum :quick_sort_by!
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ArraySort
4
+ VERSION = '0.1.0'
5
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: array-sort
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Leo Liang
8
+ autorequire:
9
+ bindir: exe
10
+ 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, insertion
56
+ sort, etc. for Arrays.
57
+ email:
58
+ - developer@leoliang.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rubocop.yml"
65
+ - ".travis.yml"
66
+ - Gemfile
67
+ - Gemfile.lock
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - array-sort.gemspec
72
+ - bin/console
73
+ - bin/setup
74
+ - 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
82
+ homepage: https://github.com/foxhatleo/array-sort
83
+ licenses:
84
+ - MIT
85
+ metadata: {}
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 2.7.6
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: Implementations of different sorting algorithms for Arrays.
106
+ test_files: []