DSA 0.0.3 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +19 -19
- data/DSA.gemspec +24 -24
- data/Gemfile +5 -5
- data/LICENSE.txt +23 -23
- data/README.md +27 -18
- data/Rakefile +2 -2
- data/lib/DSA.rb +11 -11
- data/lib/DSA/algorithm.rb +130 -0
- data/lib/DSA/version.rb +3 -3
- data/test/algorithms_test.rb +38 -6
- metadata +11 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cd9d302cc8e21bcb88c9a087cafa81fb2835b371
|
4
|
+
data.tar.gz: d44c6c4347afeab407ffed1fb7fe1dd36a1a18f2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: baffedd679af3f07429c79665a58f664bad3ef1f1dcb3ca739b518808c6f60e7319f3b7f536fd36c81fb8b78e7ecc8a7de858a8603866b54f8724a2ee221e53f
|
7
|
+
data.tar.gz: 9784aa8e128162b51b4cf84dea3738aedcfdc22bec40724c4106aa0f28f15a9218e34a06333745a29b1a07433a85dc836618ab5c2f9b282f2e8376183f319530
|
data/.gitignore
CHANGED
@@ -1,19 +1,19 @@
|
|
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
|
18
|
-
*.idea
|
19
|
-
|
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
|
18
|
+
*.idea
|
19
|
+
|
data/DSA.gemspec
CHANGED
@@ -1,24 +1,24 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
lib = File.expand_path('../lib', __FILE__)
|
3
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require 'DSA/version'
|
5
|
-
|
6
|
-
Gem::Specification.new do |spec|
|
7
|
-
spec.name = "DSA"
|
8
|
-
spec.version = DSA::VERSION
|
9
|
-
spec.authors = ["lusaisai"]
|
10
|
-
spec.email = ["lusaisai@163.com"]
|
11
|
-
spec.summary = %q{Data Structures and Algorithms in Ruby}
|
12
|
-
spec.description = %q{List, BinarySearchTree(RedBlackTree), PriorityQueue, (Array/List)Stack and Queue}
|
13
|
-
spec.homepage = "https://github.com/lusaisai/DSA"
|
14
|
-
spec.license = "MIT"
|
15
|
-
|
16
|
-
spec.files = `git ls-files -z`.split("\x0")
|
17
|
-
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
-
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
-
spec.require_paths = ["lib"]
|
20
|
-
|
21
|
-
spec.add_development_dependency "bundler", "~> 1.5"
|
22
|
-
spec.add_development_dependency "rake"
|
23
|
-
end
|
24
|
-
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'DSA/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "DSA"
|
8
|
+
spec.version = DSA::VERSION
|
9
|
+
spec.authors = ["lusaisai"]
|
10
|
+
spec.email = ["lusaisai@163.com"]
|
11
|
+
spec.summary = %q{Data Structures and Algorithms in Ruby}
|
12
|
+
spec.description = %q{List, BinarySearchTree(RedBlackTree), SkipList, PriorityQueue, (Array/List)Stack and Queue}
|
13
|
+
spec.homepage = "https://github.com/lusaisai/DSA"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
24
|
+
|
data/Gemfile
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
source 'https://rubygems.org'
|
2
|
-
|
3
|
-
# Specify your gem's dependencies in DSA.gemspec
|
4
|
-
gemspec
|
5
|
-
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in DSA.gemspec
|
4
|
+
gemspec
|
5
|
+
|
data/LICENSE.txt
CHANGED
@@ -1,23 +1,23 @@
|
|
1
|
-
Copyright (c) 2014 lusaisai
|
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.
|
23
|
-
|
1
|
+
Copyright (c) 2014 lusaisai
|
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.
|
23
|
+
|
data/README.md
CHANGED
@@ -5,28 +5,28 @@ Ruby gem for basic Data Structures and Algorithms
|
|
5
5
|
## Installation
|
6
6
|
|
7
7
|
Add this line to your application's Gemfile:
|
8
|
-
```
|
8
|
+
```
|
9
9
|
gem 'DSA'
|
10
10
|
```
|
11
11
|
And then execute:
|
12
|
-
```
|
12
|
+
```
|
13
13
|
$ bundle
|
14
14
|
```
|
15
15
|
Or install it yourself as:
|
16
|
-
```
|
16
|
+
```
|
17
17
|
$ gem install DSA
|
18
18
|
```
|
19
19
|
## Usage
|
20
20
|
|
21
21
|
To include the package,
|
22
|
-
```
|
22
|
+
```
|
23
23
|
require 'DSA'
|
24
24
|
```
|
25
25
|
|
26
26
|
### List
|
27
27
|
A doubly linked list data structure. Use when there are lots of insertions/deletions in the middle,
|
28
28
|
otherwise, built-in array is better.
|
29
|
-
```
|
29
|
+
```
|
30
30
|
l = DSA::List.new
|
31
31
|
l.push 'some value'
|
32
32
|
l.pop
|
@@ -38,13 +38,13 @@ otherwise, built-in array is better.
|
|
38
38
|
l.length
|
39
39
|
```
|
40
40
|
General access/removal/insertion using index is supported, these operations require linear time, so use carefully.
|
41
|
-
```
|
41
|
+
```
|
42
42
|
l[2]
|
43
43
|
l.insert_at 10, 'some value'
|
44
44
|
l.remove_at 2
|
45
45
|
```
|
46
46
|
To do lots of insertions/deletions, use the iterator, StopIteration is raised when reaching to head or tail
|
47
|
-
```
|
47
|
+
```
|
48
48
|
li = l.begin_iterator # the iterator starts from the head
|
49
49
|
puts li.next
|
50
50
|
li.insert 'some value'
|
@@ -61,19 +61,19 @@ the performance might not be the best, use only when a full traversal is inevita
|
|
61
61
|
An ordered map, works like a hash, but preserves an order and provides range search, implemented as a RedBlack tree.
|
62
62
|
|
63
63
|
The following three are aliases in creating a new object,
|
64
|
-
```
|
64
|
+
```
|
65
65
|
rb = DSA::BinarySearchTree.new
|
66
66
|
rb = DSA::OrderedMap.new
|
67
67
|
rb = DSA::RedBlackTree.new
|
68
68
|
```
|
69
69
|
Method are very like a hash,
|
70
|
-
```
|
70
|
+
```
|
71
71
|
rb[key] = value
|
72
72
|
rb[key]
|
73
73
|
rb.delete key
|
74
74
|
```
|
75
75
|
And special methods related to orders, those methods yield key/value pairs to block, if no block, enumerator is returned.
|
76
|
-
```
|
76
|
+
```
|
77
77
|
rb.each # in-order traversal
|
78
78
|
rb.gt(key) # key/value pairs for keys greater than key
|
79
79
|
rb.ge(key)
|
@@ -81,7 +81,7 @@ And special methods related to orders, those methods yield key/value pairs to bl
|
|
81
81
|
rb.le(key)
|
82
82
|
```
|
83
83
|
A help method tried to print a tree, not quite pretty, but may helps test
|
84
|
-
```
|
84
|
+
```
|
85
85
|
rb.bfs_print
|
86
86
|
```
|
87
87
|
Enumerable is included, all those method such as 'each' are all available, since other methods are based on each,
|
@@ -90,7 +90,7 @@ the performance might not be the best, use only when a full traversal is inevita
|
|
90
90
|
### SkipList
|
91
91
|
An ordered map, storing key/value pairs, duplicate keys are allowed, value can be omitted, preserves an order and provides range search, implemented as a skip list.
|
92
92
|
|
93
|
-
```
|
93
|
+
```
|
94
94
|
sl = DSA::SkipList.new
|
95
95
|
sl.add key, value
|
96
96
|
sl.add key # value will be nil
|
@@ -99,7 +99,7 @@ An ordered map, storing key/value pairs, duplicate keys are allowed, value can b
|
|
99
99
|
```
|
100
100
|
|
101
101
|
And special methods related to orders, those methods yield key/value pairs to block, if no block, enumerator is returned.
|
102
|
-
```
|
102
|
+
```
|
103
103
|
sl.find key
|
104
104
|
sl.each # in-order traversal
|
105
105
|
sl.gt(key) # key/value pairs for keys greater than key
|
@@ -108,7 +108,7 @@ And special methods related to orders, those methods yield key/value pairs to bl
|
|
108
108
|
sl.le(key)
|
109
109
|
```
|
110
110
|
A help method prints the skip list
|
111
|
-
```
|
111
|
+
```
|
112
112
|
sl.print_me
|
113
113
|
sl.print_me width # width, the length of evert key/value pair, default to 10
|
114
114
|
```
|
@@ -118,7 +118,7 @@ the performance might not be the best, use only when a full traversal is inevita
|
|
118
118
|
|
119
119
|
### PriorityQueue
|
120
120
|
An array based heap, priority is a number, the smaller it is, higher priority it has
|
121
|
-
```
|
121
|
+
```
|
122
122
|
pq = DSA::PriorityQueue.new
|
123
123
|
pq.add 10, 'some job'
|
124
124
|
pq.length
|
@@ -128,7 +128,7 @@ An array based heap, priority is a number, the smaller it is, higher priority it
|
|
128
128
|
|
129
129
|
### Stack and Queue
|
130
130
|
Implemented based on array or list.
|
131
|
-
```
|
131
|
+
```
|
132
132
|
s = DSA::ArrayStack.new
|
133
133
|
s = DSA::ListStack.new
|
134
134
|
s.push 'some value'
|
@@ -136,7 +136,8 @@ Implemented based on array or list.
|
|
136
136
|
s.empty?
|
137
137
|
s.top
|
138
138
|
s.length
|
139
|
-
|
139
|
+
```
|
140
|
+
```
|
140
141
|
q = DSA::ArrayQueue.new
|
141
142
|
q = DSA::ListQueue.new
|
142
143
|
q.enqueue 'some value'
|
@@ -148,9 +149,17 @@ Implemented based on array or list.
|
|
148
149
|
### Algorithm
|
149
150
|
The following functions are for demonstrations, specially sort, using built-in Array#bsearch and Array#sort instead,
|
150
151
|
they have a better performance.
|
151
|
-
```
|
152
|
+
```
|
152
153
|
DSA::Algorithm::factorial(5)
|
154
|
+
DSA::Algorithm::fibonacci(10) # There are different implementations with different performance, defaults to fibonacci_logarithm(10)
|
155
|
+
DSA::Algorithm::fibonacci_exponential(10)
|
156
|
+
DSA::Algorithm::fibonacci_linear(10)
|
157
|
+
DSA::Algorithm::fibonacci_logarithm(10)
|
158
|
+
DSA::Algorithm::fibonacci_constant(10)
|
159
|
+
DSA::Algorithm::fibonacci_bad(10)
|
153
160
|
DSA::Algorithm::binary_search((1..9).to_a, 2, 0, 8)
|
154
161
|
DSA::Algorithm::insertion_sort!(array)
|
155
162
|
DSA::Algorithm::quick_sort!(array, 0, array.length-1)
|
163
|
+
DSA::Algorithm::radix_sort(array) # array should be integers
|
164
|
+
DSA::Algorithm::sqrt(number)
|
156
165
|
```
|
data/Rakefile
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
require "bundler/gem_tasks"
|
2
|
-
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
|
data/lib/DSA.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
|
-
require_relative 'DSA/version'
|
2
|
-
require_relative 'DSA/algorithm'
|
3
|
-
require_relative 'DSA/stack_and_queue'
|
4
|
-
require_relative 'DSA/list'
|
5
|
-
require_relative 'DSA/priority_queue'
|
6
|
-
require_relative 'DSA/binary_search_tree'
|
7
|
-
require_relative 'DSA/skip_list'
|
8
|
-
|
9
|
-
module DSA
|
10
|
-
# Your code goes here...
|
11
|
-
end
|
1
|
+
require_relative 'DSA/version'
|
2
|
+
require_relative 'DSA/algorithm'
|
3
|
+
require_relative 'DSA/stack_and_queue'
|
4
|
+
require_relative 'DSA/list'
|
5
|
+
require_relative 'DSA/priority_queue'
|
6
|
+
require_relative 'DSA/binary_search_tree'
|
7
|
+
require_relative 'DSA/skip_list'
|
8
|
+
|
9
|
+
module DSA
|
10
|
+
# Your code goes here...
|
11
|
+
end
|
data/lib/DSA/algorithm.rb
CHANGED
@@ -11,6 +11,102 @@ module DSA
|
|
11
11
|
end
|
12
12
|
|
13
13
|
|
14
|
+
def self.fibonacci(n)
|
15
|
+
self.fibonacci_logarithm(n)
|
16
|
+
end
|
17
|
+
|
18
|
+
# we could implement fibonacci using recursion which looks simple, unfortunately, the running time of this algorithm
|
19
|
+
# itself is a fibonacci sequence, which grows fast, kind of exponential
|
20
|
+
def self.fibonacci_exponential(n)
|
21
|
+
if !n.is_a? Integer or n < 0
|
22
|
+
raise ArgumentError
|
23
|
+
end
|
24
|
+
|
25
|
+
if n == 0
|
26
|
+
0
|
27
|
+
elsif n == 1
|
28
|
+
1
|
29
|
+
elsif n >= 2
|
30
|
+
fibonacci_exponential(n-1) + fibonacci_exponential(n-2)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
# fibonacci numbers
|
36
|
+
def self.fibonacci_linear(n)
|
37
|
+
if !n.is_a? Integer or n < 0
|
38
|
+
raise ArgumentError
|
39
|
+
end
|
40
|
+
|
41
|
+
if n == 0
|
42
|
+
0
|
43
|
+
elsif n == 1
|
44
|
+
1
|
45
|
+
elsif n >= 2
|
46
|
+
a = 0
|
47
|
+
b = 1
|
48
|
+
2.upto(n) {
|
49
|
+
b, a = a + b, b
|
50
|
+
}
|
51
|
+
b
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
# fibonacci sequences can be calculated in matrix multiplying
|
56
|
+
# [[f(n+1), f(n)], [f(n), f(n-1)]] equals [[1,1], [1,0]] to the nth
|
57
|
+
# matrix multiplying can be achieved in logarithm time if we use divide and conquer
|
58
|
+
def self.fibonacci_logarithm(n)
|
59
|
+
if !n.is_a? Integer or n < 0
|
60
|
+
raise ArgumentError
|
61
|
+
end
|
62
|
+
|
63
|
+
if n == 0
|
64
|
+
0
|
65
|
+
elsif n == 1
|
66
|
+
1
|
67
|
+
elsif n >= 2
|
68
|
+
result = self.fibonacci_matrix_nth(n)
|
69
|
+
result[0][1]
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def self.fibonacci_matrix_nth(n)
|
74
|
+
first = [[1,1], [1,0]]
|
75
|
+
if n == 1
|
76
|
+
first
|
77
|
+
elsif n % 2 == 0
|
78
|
+
half = self.fibonacci_matrix_nth(n/2)
|
79
|
+
square_matrix_multiply(half, half)
|
80
|
+
else
|
81
|
+
square_matrix_multiply(first, self.fibonacci_matrix_nth(n-1))
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def self.square_matrix_multiply(a, b, size=2)
|
86
|
+
result = []
|
87
|
+
n = size - 1
|
88
|
+
0.upto(n) do |i|
|
89
|
+
result[i] = []
|
90
|
+
0.upto(n) do |j|
|
91
|
+
result[i][j] = 0
|
92
|
+
0.upto(n) do |k|
|
93
|
+
result[i][j] += a[i][k] * b[k][j]
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
result
|
98
|
+
end
|
99
|
+
|
100
|
+
|
101
|
+
# It is kind of cheating, but mathematicians are unbelievable,
|
102
|
+
# they figured out a function to calculate the fibonacci sequence directly.
|
103
|
+
# There's limitation on the 64-bit float numbers, so only use it to calculate small numbers.
|
104
|
+
def self.fibonacci_constant(n)
|
105
|
+
phi = 1.618034
|
106
|
+
((phi ** n - (1-phi) ** n) / Math.sqrt(5)).round
|
107
|
+
end
|
108
|
+
|
109
|
+
|
14
110
|
# Binary search in sorted array
|
15
111
|
def self.binary_search(data, target, low, high)
|
16
112
|
return false if low > high
|
@@ -64,5 +160,39 @@ module DSA
|
|
64
160
|
quick_sort!(data, left, high)
|
65
161
|
end
|
66
162
|
|
163
|
+
# radix sort, in base 10
|
164
|
+
def self.radix_sort(data)
|
165
|
+
goto_next_digit = true
|
166
|
+
position = 0
|
167
|
+
while goto_next_digit
|
168
|
+
goto_next_digit = false
|
169
|
+
buckets = []
|
170
|
+
10.times { buckets << [] }
|
171
|
+
data.each do |number|
|
172
|
+
digit = get_digit(number, position)
|
173
|
+
goto_next_digit = true if digit > 0 # if all digits are zero, loop will end
|
174
|
+
buckets[digit] << number
|
175
|
+
end
|
176
|
+
data = buckets.flatten!
|
177
|
+
position += 1
|
178
|
+
end
|
179
|
+
data
|
180
|
+
end
|
181
|
+
|
182
|
+
def self.sqrt(n)
|
183
|
+
err = 1e-10
|
184
|
+
r = Float(n)
|
185
|
+
while (r - n/r).abs >= err
|
186
|
+
r = ( r + n/r ) / 2
|
187
|
+
end
|
188
|
+
r
|
189
|
+
end
|
190
|
+
|
191
|
+
|
192
|
+
private
|
193
|
+
def self.get_digit(number, position)
|
194
|
+
number % (10 ** (position+1)) / (10 ** position)
|
195
|
+
end
|
196
|
+
|
67
197
|
end
|
68
198
|
end
|
data/lib/DSA/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
module DSA
|
2
|
-
VERSION =
|
3
|
-
end
|
1
|
+
module DSA
|
2
|
+
VERSION = '1.0.0'
|
3
|
+
end
|
data/test/algorithms_test.rb
CHANGED
@@ -17,11 +17,41 @@ class MyTest < Test::Unit::TestCase
|
|
17
17
|
# Do nothing
|
18
18
|
end
|
19
19
|
|
20
|
+
def test_sqrt
|
21
|
+
puts 'square root of 2 is ', DSA::Algorithm::sqrt(2)
|
22
|
+
puts 'square root of 25 is ', DSA::Algorithm::sqrt(25)
|
23
|
+
puts 'square root of 123456.789 is ', DSA::Algorithm::sqrt(123456.789)
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_radix_sort
|
27
|
+
assert_equal( 2, DSA::Algorithm::get_digit(123, 1), 'get digit failed' )
|
28
|
+
assert_equal( 0, DSA::Algorithm::get_digit(123, 5), 'get digit failed' )
|
29
|
+
original = [123,6,7890,88,32,254,345677,238,1,125,29,226]
|
30
|
+
expected = [1, 6, 29, 32, 88, 123, 125, 226, 238, 254, 7890, 345677]
|
31
|
+
assert_equal expected, DSA::Algorithm::radix_sort(original), 'sort failed'
|
32
|
+
end
|
33
|
+
|
20
34
|
def test_function
|
21
35
|
assert_equal 120, DSA::Algorithm::factorial(5), 'factorial function is wrong'
|
22
36
|
assert DSA::Algorithm::binary_search((1..9).to_a, 2, 0, 8), 'binary search failed'
|
37
|
+
assert_equal 55, DSA::Algorithm::fibonacci(10), 'fibonacci failed'
|
23
38
|
end
|
24
39
|
|
40
|
+
def test_fibonacci
|
41
|
+
assert_equal 55, DSA::Algorithm::fibonacci_exponential(10), 'fibonacci_exponential failed'
|
42
|
+
assert_equal 55, DSA::Algorithm::fibonacci_linear(10), 'fibonacci_linear failed'
|
43
|
+
assert_equal 55, DSA::Algorithm::fibonacci_logarithm(10), 'fibonacci_logarithm failed'
|
44
|
+
assert_equal 55, DSA::Algorithm::fibonacci_constant(10), 'fibonacci_constant failed'
|
45
|
+
Benchmark.bm(20) do |x|
|
46
|
+
# x.report('fibonacci exponential') { DSA::Algorithm::fibonacci_exponential(35) }
|
47
|
+
x.report('fibonacci linear') { DSA::Algorithm::fibonacci_linear(99999) }
|
48
|
+
x.report('fibonacci logarithm') { DSA::Algorithm::fibonacci_logarithm(99999) }
|
49
|
+
x.report('fibonacci constant') { DSA::Algorithm::fibonacci_constant(1000) }
|
50
|
+
# x.report('fibonacci constant') { DSA::Algorithm::fibonacci_constant(100000) }
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
|
25
55
|
def test_performance
|
26
56
|
puts
|
27
57
|
|
@@ -50,11 +80,8 @@ class MyTest < Test::Unit::TestCase
|
|
50
80
|
end
|
51
81
|
|
52
82
|
def test_sort_performance
|
53
|
-
|
54
|
-
|
55
|
-
|
56
83
|
puts 'sort on already sorted'
|
57
|
-
puts
|
84
|
+
puts '=' * 50
|
58
85
|
value = 10**5
|
59
86
|
sorted_a = (0..value).to_a
|
60
87
|
sorted_b = sorted_a.map {|e| e}
|
@@ -69,8 +96,9 @@ class MyTest < Test::Unit::TestCase
|
|
69
96
|
assert_equal sorted_a, sorted_b, ''
|
70
97
|
assert_equal sorted_a, sorted_c, ''
|
71
98
|
|
72
|
-
|
99
|
+
puts
|
73
100
|
puts 'sort on not sorted'
|
101
|
+
puts '=' * 50
|
74
102
|
value = 10**4
|
75
103
|
not_sorted_a = value.times.map { Random.rand(value) }
|
76
104
|
not_sorted_b = not_sorted_a.map { |e| e }
|
@@ -83,12 +111,16 @@ class MyTest < Test::Unit::TestCase
|
|
83
111
|
assert_equal not_sorted_a, not_sorted_b, ''
|
84
112
|
assert_equal not_sorted_a, not_sorted_c, ''
|
85
113
|
|
86
|
-
puts
|
114
|
+
puts
|
115
|
+
puts 'sort on a million not sorted'
|
116
|
+
puts '=' * 50
|
87
117
|
value = 10**6
|
88
118
|
not_sorted_b = value.times.map { Random.rand(value) }
|
89
119
|
not_sorted_c = value.times.map { Random.rand(value) }
|
120
|
+
not_sorted_d = value.times.map { Random.rand(value) }
|
90
121
|
Benchmark.bm(20) do |x|
|
91
122
|
x.report('quick sort') { DSA::Algorithm::quick_sort! not_sorted_c, 0, not_sorted_c.length-1 }
|
123
|
+
x.report('radix sort') { DSA::Algorithm::radix_sort not_sorted_d }
|
92
124
|
x.report('built in sort') { not_sorted_b.sort! }
|
93
125
|
end
|
94
126
|
|
metadata
CHANGED
@@ -1,44 +1,44 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: DSA
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- lusaisai
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-03-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - ~>
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '1.5'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - ~>
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.5'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
-
description: List, BinarySearchTree(RedBlackTree), PriorityQueue, (Array/List)Stack
|
41
|
+
description: List, BinarySearchTree(RedBlackTree), SkipList, PriorityQueue, (Array/List)Stack
|
42
42
|
and Queue
|
43
43
|
email:
|
44
44
|
- lusaisai@163.com
|
@@ -46,7 +46,7 @@ executables: []
|
|
46
46
|
extensions: []
|
47
47
|
extra_rdoc_files: []
|
48
48
|
files:
|
49
|
-
- .gitignore
|
49
|
+
- ".gitignore"
|
50
50
|
- DSA.gemspec
|
51
51
|
- Gemfile
|
52
52
|
- LICENSE.txt
|
@@ -76,17 +76,17 @@ require_paths:
|
|
76
76
|
- lib
|
77
77
|
required_ruby_version: !ruby/object:Gem::Requirement
|
78
78
|
requirements:
|
79
|
-
- -
|
79
|
+
- - ">="
|
80
80
|
- !ruby/object:Gem::Version
|
81
81
|
version: '0'
|
82
82
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
83
|
requirements:
|
84
|
-
- -
|
84
|
+
- - ">="
|
85
85
|
- !ruby/object:Gem::Version
|
86
86
|
version: '0'
|
87
87
|
requirements: []
|
88
88
|
rubyforge_project:
|
89
|
-
rubygems_version: 2.
|
89
|
+
rubygems_version: 2.2.2
|
90
90
|
signing_key:
|
91
91
|
specification_version: 4
|
92
92
|
summary: Data Structures and Algorithms in Ruby
|