inversions_in_array 0.0.1

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.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in inversion_of_array.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Alexander Brausch
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # InversionsInArray
2
+
3
+ Count all inversions in an array. For example if you have an array with the following value:
4
+
5
+ [1,3,5,2,4,6]
6
+
7
+ You have three inversion [3,2], [5,2] and [5,4]
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'inversions_in_array'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install inversions_in_array
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Contributing
28
+
29
+ 1. Fork it
30
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
31
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
32
+ 4. Push to the branch (`git push origin my-new-feature`)
33
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+
7
+ task :default => :spec
8
+ task :test => :spec
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'benchmark'
4
+ require 'inversion_of_array.rb'
5
+
6
+ array = []
7
+ for i in 1..10000
8
+ array << i
9
+ end
10
+ array.shuffle!
11
+
12
+ time = Benchmark.measure do
13
+ inversions = InversionOfArray.inversions(BruteForceStrategy.new,array)
14
+ p "Inversions #{inversions}"
15
+ end
16
+
17
+ p time
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'benchmark'
4
+ require 'inversion_of_array.rb'
5
+
6
+ array = []
7
+ for i in 1..10000
8
+ array << i
9
+ end
10
+ array.shuffle!
11
+
12
+ time = Benchmark.measure do
13
+ inversions = InversionOfArray.inversions(DivideAndConquerStrategy.new,array)
14
+ p "Inversions #{inversions}"
15
+ end
16
+
17
+ p time
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/inversions_in_array/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Alexander Brausch"]
6
+ gem.email = ["abrausch@web.de"]
7
+ gem.description = %q{Calculate the inversions in an array}
8
+ gem.summary = %q{Calculate the inversions in an array}
9
+ gem.homepage = "https://github.com/abrausch/inversions_in_array"
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "inversions_in_array"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = InversionsInArray::VERSION
17
+
18
+ gem.add_development_dependency "rspec"
19
+ gem.add_development_dependency "rake"
20
+ end
@@ -0,0 +1,14 @@
1
+ class BruteForceStrategy
2
+ def inversions(array)
3
+ inversions = 0
4
+ for i in 0..array.size-1
5
+ current_value = array[i]
6
+ for j in i+1..array.size-1
7
+ if current_value > array[j]
8
+ inversions = inversions + 1
9
+ end
10
+ end
11
+ end
12
+ inversions
13
+ end
14
+ end
@@ -0,0 +1,33 @@
1
+ class DivideAndConquerStrategy
2
+ def merge(left,right)
3
+ sorted = []
4
+ inversions = 0
5
+ until left.empty? or right.empty?
6
+ if left.first <= right.first
7
+ sorted << left.shift
8
+ else
9
+ sorted << right.shift
10
+
11
+ if !left.empty?
12
+ inversions = inversions + left.size
13
+ end
14
+ end
15
+ end
16
+
17
+ return inversions, sorted.concat(left).concat(right)
18
+ end
19
+
20
+ def sort_and_calculate_inversions(array)
21
+ return 0, array if array.size <= 1
22
+
23
+ mid = array.size / 2
24
+ left = array[0,mid]
25
+ right = array[mid,array.size - mid]
26
+
27
+ merge(sort_and_calculate_inversions(left)[1],sort_and_calculate_inversions(right)[1])
28
+ end
29
+
30
+ def inversions(array)
31
+ sort_and_calculate_inversions(array)[0]
32
+ end
33
+ end
@@ -0,0 +1,11 @@
1
+ module InversionsInArray
2
+ class InversionsInArrayApi
3
+ def initialize (strategy)
4
+ @strategy = strategy
5
+ end
6
+
7
+ def inversions(array)
8
+ @strategy.inversions(array)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module InversionsInArray
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,10 @@
1
+ require "inversions_in_array/version"
2
+ require "inversions_in_array/inversions_in_array_api"
3
+ require "inversions_in_array/brute_force_strategy"
4
+ require "inversions_in_array/divide_and_conquer_strategy"
5
+
6
+ module InversionsInArray
7
+ def self.inversions (strategy,array)
8
+ InversionsInArrayApi.new(strategy).inversions(array)
9
+ end
10
+ end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+
3
+ describe InversionsInArray do
4
+ it "should use brute_force and find three inversions" do
5
+ array = [1,3,5,2,4,6]
6
+ inversions = InversionsInArray.inversions(BruteForceStrategy.new,array)
7
+ inversions.should == 3
8
+ end
9
+ it "should use divide_and_conquer and find three inversions" do
10
+ array = [1,3,5,2,4,6]
11
+ inversions = InversionsInArray.inversions(DivideAndConquerStrategy.new,array)
12
+ inversions.should == 3
13
+ end
14
+ end
@@ -0,0 +1 @@
1
+ require 'inversions_in_array'
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: inversions_in_array
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alexander Brausch
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70106101127120 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70106101127120
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &70106101126520 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70106101126520
36
+ description: Calculate the inversions in an array
37
+ email:
38
+ - abrausch@web.de
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - .gitignore
44
+ - Gemfile
45
+ - LICENSE
46
+ - README.md
47
+ - Rakefile
48
+ - benchmark/brute_force.rb
49
+ - benchmark/divide_conquer.rb
50
+ - inversions_in_array.gemspec
51
+ - lib/inversions_in_array.rb
52
+ - lib/inversions_in_array/brute_force_strategy.rb
53
+ - lib/inversions_in_array/divide_and_conquer_strategy.rb
54
+ - lib/inversions_in_array/inversions_in_array_api.rb
55
+ - lib/inversions_in_array/version.rb
56
+ - spec/inversions_in_array_spec.rb
57
+ - spec/spec_helper.rb
58
+ homepage: https://github.com/abrausch/inversions_in_array
59
+ licenses: []
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ segments:
71
+ - 0
72
+ hash: 4566840598292712094
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ segments:
80
+ - 0
81
+ hash: 4566840598292712094
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 1.8.11
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: Calculate the inversions in an array
88
+ test_files:
89
+ - spec/inversions_in_array_spec.rb
90
+ - spec/spec_helper.rb