expertsort 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 +4 -0
- data/Gemfile +4 -0
- data/LICENSE +21 -0
- data/README.md +56 -0
- data/Rakefile +1 -0
- data/expertsort.gemspec +27 -0
- data/lib/expertsort.rb +13 -0
- data/lib/expertsort/sorts/bogosort.rb +16 -0
- data/lib/expertsort/sorts/sleepsort.rb +25 -0
- data/lib/expertsort/sorts/slowestsort.rb +12 -0
- data/lib/expertsort/sorts/stoogesort.rb +23 -0
- data/lib/expertsort/version.rb +3 -0
- data/spec/expertsort/expertsort_spec.rb +78 -0
- metadata +75 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2013 Ng Guoyou
|
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,56 @@
|
|
1
|
+
# EXPERTSORT
|
2
|
+
|
3
|
+
Expertsort (stylised EXPERTSORT) is a collection of EXPERT PROGRAMMER sorting algorithms.
|
4
|
+
|
5
|
+
Ever felt that sorting in Ruby is too fast?
|
6
|
+
|
7
|
+
Need a slow algorithm for v0.0.1 of your application?
|
8
|
+
|
9
|
+
Is your hardware too fast?
|
10
|
+
|
11
|
+
Want to prove that sorting in Ruby is slow as balls?
|
12
|
+
|
13
|
+
EXPERTSORT is here to help.
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
Add this line to your application's Gemfile:
|
18
|
+
|
19
|
+
gem 'expertsort'
|
20
|
+
|
21
|
+
And then execute:
|
22
|
+
|
23
|
+
$ bundle
|
24
|
+
|
25
|
+
Or install it yourself as:
|
26
|
+
|
27
|
+
$ gem install expertsort
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
[5, 4, 3, 1, 2].bogosort
|
32
|
+
=> [1, 2, 3, 4, 5]
|
33
|
+
|
34
|
+
## Sorts
|
35
|
+
|
36
|
+
### Bogosort
|
37
|
+
|
38
|
+
* `bogosort`, `bogosort!`
|
39
|
+
|
40
|
+
### Stoogesort
|
41
|
+
|
42
|
+
* `stoogesort`, `stoogesort!`
|
43
|
+
|
44
|
+
### Slowestsort
|
45
|
+
|
46
|
+
* `slowestsort`, `slowestsort!`
|
47
|
+
|
48
|
+
### Sleepsort
|
49
|
+
|
50
|
+
* `sleepsort`, `sleepsort!`
|
51
|
+
|
52
|
+
Sleepsort raises a `RangeError` if the array contains a negative element.
|
53
|
+
|
54
|
+
## Testing
|
55
|
+
|
56
|
+
There is a rspec for EXPERTSORT at `spec/expertsort/expertsort_spec.rb`.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/expertsort.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "expertsort/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "expertsort"
|
7
|
+
s.version = Expertsort::VERSION
|
8
|
+
s.authors = ["Ng Guoyou"]
|
9
|
+
s.email = ["fauxface+github@gmail.com"]
|
10
|
+
s.homepage = "https://github.com/gyng/expertsort"
|
11
|
+
s.summary = %q{A collection of EXPERT PROGRAMMER sorting algorithms.}
|
12
|
+
s.description = %q{Expertsort (stylised EXPERTSORT) is a collection of EXPERT PROGRAMMER sorting algorithms.
|
13
|
+
Ever felt that sorting in Ruby is too fast?
|
14
|
+
Need a slow algorithm for v0.0.1 of your application? (So you can show your boss that you, programmer extraordinaire, significantly improved performance in v0.0.2)
|
15
|
+
Want to prove that sorting in Ruby is slow as balls?
|
16
|
+
EXPERTSORT is here to help.}
|
17
|
+
s.license = "MIT"
|
18
|
+
|
19
|
+
s.rubyforge_project = "expertsort"
|
20
|
+
|
21
|
+
s.files = `git ls-files`.split("\n")
|
22
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
23
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
24
|
+
s.require_paths = ["lib"]
|
25
|
+
|
26
|
+
s.add_development_dependency "rspec"
|
27
|
+
end
|
data/lib/expertsort.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require "expertsort/version"
|
2
|
+
|
3
|
+
require 'expertsort/sorts/bogosort'
|
4
|
+
require 'expertsort/sorts/sleepsort'
|
5
|
+
require 'expertsort/sorts/slowestsort'
|
6
|
+
require 'expertsort/sorts/stoogesort'
|
7
|
+
|
8
|
+
class Array
|
9
|
+
include Expertsort::BogoSort
|
10
|
+
include Expertsort::SleepSort
|
11
|
+
include Expertsort::SlowestSort
|
12
|
+
include Expertsort::StoogeSort
|
13
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Expertsort
|
2
|
+
module SleepSort
|
3
|
+
def sleepsort
|
4
|
+
sorted = []
|
5
|
+
threads = []
|
6
|
+
semaphore = Mutex.new
|
7
|
+
|
8
|
+
# Reduce potential impact of thread creation time by joining them when all threads have been created
|
9
|
+
self.each do |e|
|
10
|
+
raise RangeError, "Cannot sleep sort an array with negative elements: #{e}" if e.to_i < 0
|
11
|
+
threads << Thread.new do
|
12
|
+
sleep e.to_i
|
13
|
+
semaphore.synchronize { sorted << e.to_i }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
threads.each { |t| t.join }
|
18
|
+
sorted
|
19
|
+
end
|
20
|
+
|
21
|
+
def sleepsort!
|
22
|
+
self.replace(sleepsort)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Expertsort
|
2
|
+
module StoogeSort
|
3
|
+
# Straight up reimplementation of the algorithm on Wikipedia.
|
4
|
+
def stoogesort
|
5
|
+
self.dup.stoogesort!
|
6
|
+
end
|
7
|
+
|
8
|
+
def stoogesort!(i = 0, j = self.length-1)
|
9
|
+
if self[j] < self[i]
|
10
|
+
self[i], self[j] = self[j], self[i]
|
11
|
+
end
|
12
|
+
|
13
|
+
if j - i + 1 >= 3
|
14
|
+
t = (j - i + 1) / 3
|
15
|
+
stoogesort!(i, j - t)
|
16
|
+
stoogesort!(i + t, j)
|
17
|
+
stoogesort!(i, j - t)
|
18
|
+
end
|
19
|
+
|
20
|
+
self
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'expertsort'
|
2
|
+
|
3
|
+
describe Array do
|
4
|
+
before do
|
5
|
+
@unsorted = [5, 4, 3, 1, 2]
|
6
|
+
@sorted = @unsorted.sort
|
7
|
+
end
|
8
|
+
|
9
|
+
describe 'BogoSort' do
|
10
|
+
it 'sorts' do
|
11
|
+
expect(@unsorted.bogosort).to eq(@sorted)
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'has a destuctive method' do
|
15
|
+
array = @unsorted.dup
|
16
|
+
old_object_id = array.object_id
|
17
|
+
array.bogosort!
|
18
|
+
expect(old_object_id).to eq(array.object_id)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe 'StoogeSort' do
|
23
|
+
it 'sorts' do
|
24
|
+
expect(@unsorted.stoogesort).to eq(@sorted)
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'has a destuctive method' do
|
28
|
+
array = @unsorted.dup
|
29
|
+
old_object_id = array.object_id
|
30
|
+
array.stoogesort!
|
31
|
+
expect(old_object_id).to eq(array.object_id)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe 'SleepSort' do
|
36
|
+
it 'sorts' do
|
37
|
+
expect(@unsorted.sleepsort).to eq(@sorted)
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'has a destuctive method' do
|
41
|
+
array = @unsorted.dup
|
42
|
+
old_object_id = array.object_id
|
43
|
+
array.sleepsort!
|
44
|
+
expect(old_object_id).to eq(array.object_id)
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'takes a long time' do
|
48
|
+
start_time = Time.now
|
49
|
+
@unsorted.sleepsort
|
50
|
+
expect(Time.now).to be > (start_time + @unsorted.max)
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'raises an error if there is a negative element' do
|
54
|
+
bad_data = [1, 2, 0, -1, -2]
|
55
|
+
expect{bad_data.sleepsort}.to raise_error(RangeError, 'Cannot sleep sort an array with negative elements: -1')
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe 'SlowestSort' do
|
60
|
+
it 'sorts' do
|
61
|
+
expect(@unsorted.slowestsort).to eq(@sorted)
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'has a destuctive method' do
|
65
|
+
array = @unsorted.dup
|
66
|
+
old_object_id = array.object_id
|
67
|
+
array.slowestsort!
|
68
|
+
expect(old_object_id).to eq(array.object_id)
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'takes a very long time' do
|
72
|
+
start_time = Time.now
|
73
|
+
@unsorted.slowestsort
|
74
|
+
sleep(1)
|
75
|
+
expect(Time.now).to be > start_time + 1
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: expertsort
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ng Guoyou
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-08-15 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &25494828 !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: *25494828
|
25
|
+
description: ! "Expertsort (stylised EXPERTSORT) is a collection of EXPERT PROGRAMMER
|
26
|
+
sorting algorithms.\n Ever felt that sorting in Ruby is too
|
27
|
+
fast?\n Need a slow algorithm for v0.0.1 of your application?
|
28
|
+
(So you can show your boss that you, programmer extraordinaire, significantly improved
|
29
|
+
performance in v0.0.2)\n Want to prove that sorting in Ruby
|
30
|
+
is slow as balls?\n EXPERTSORT is here to help."
|
31
|
+
email:
|
32
|
+
- fauxface+github@gmail.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- Gemfile
|
39
|
+
- LICENSE
|
40
|
+
- README.md
|
41
|
+
- Rakefile
|
42
|
+
- expertsort.gemspec
|
43
|
+
- lib/expertsort.rb
|
44
|
+
- lib/expertsort/sorts/bogosort.rb
|
45
|
+
- lib/expertsort/sorts/sleepsort.rb
|
46
|
+
- lib/expertsort/sorts/slowestsort.rb
|
47
|
+
- lib/expertsort/sorts/stoogesort.rb
|
48
|
+
- lib/expertsort/version.rb
|
49
|
+
- spec/expertsort/expertsort_spec.rb
|
50
|
+
homepage: https://github.com/gyng/expertsort
|
51
|
+
licenses:
|
52
|
+
- MIT
|
53
|
+
post_install_message:
|
54
|
+
rdoc_options: []
|
55
|
+
require_paths:
|
56
|
+
- lib
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ! '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
65
|
+
requirements:
|
66
|
+
- - ! '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
requirements: []
|
70
|
+
rubyforge_project: expertsort
|
71
|
+
rubygems_version: 1.8.16
|
72
|
+
signing_key:
|
73
|
+
specification_version: 3
|
74
|
+
summary: A collection of EXPERT PROGRAMMER sorting algorithms.
|
75
|
+
test_files: []
|