algorithm_selector 0.1.1 → 0.1.2
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 +4 -4
- data/.gitignore +1 -0
- data/.yardoc/checksums +2 -2
- data/.yardoc/objects/root.dat +0 -0
- data/README.md +145 -34
- data/algorithm_selector.gemspec +2 -2
- data/doc/AlgorithmSelector.html +71 -60
- data/doc/BSTNode.html +19 -19
- data/doc/BinaryTree.html +46 -46
- data/doc/LinkNode.html +19 -19
- data/doc/LinkedList.html +72 -72
- data/doc/Queue.html +55 -68
- data/doc/Searches.html +141 -78
- data/doc/Sorts.html +124 -123
- data/doc/Stack.html +64 -77
- data/doc/_index.html +1 -1
- data/doc/file.README.html +147 -22
- data/doc/index.html +147 -22
- data/doc/top-level-namespace.html +1 -1
- data/lib/algorithm_selector.rb +118 -108
- data/lib/algorithm_selector/version.rb +1 -1
- metadata +4 -4
@@ -104,7 +104,7 @@
|
|
104
104
|
</div>
|
105
105
|
|
106
106
|
<div id="footer">
|
107
|
-
Generated on
|
107
|
+
Generated on Sat Jul 23 23:26:27 2016 by
|
108
108
|
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
109
109
|
0.9.4 (ruby-2.1.2).
|
110
110
|
</div>
|
data/lib/algorithm_selector.rb
CHANGED
@@ -1,71 +1,75 @@
|
|
1
1
|
require "algorithm_selector/version"
|
2
2
|
|
3
|
+
# algorithm-selector library; 5 methods (all, best, analyze, compare, sort)
|
3
4
|
module AlgorithmSelector
|
4
5
|
extend self
|
5
6
|
|
6
|
-
#
|
7
|
-
def all(
|
8
|
-
if
|
9
|
-
Sorts.new.all(
|
10
|
-
elsif
|
11
|
-
Searches.new.all(
|
7
|
+
# Returns the times for each algorithm
|
8
|
+
def all(action, data_set, trials=1, target=nil)
|
9
|
+
if action == "sort"
|
10
|
+
Sorts.new.all(data_set, trials)
|
11
|
+
elsif action == "search"
|
12
|
+
Searches.new.all(data_set, trials, target)
|
12
13
|
end
|
13
14
|
end
|
14
15
|
|
15
|
-
#
|
16
|
-
def best(
|
17
|
-
if
|
18
|
-
Sorts.new.best(
|
19
|
-
elsif
|
20
|
-
Searches.new.best(
|
16
|
+
# Returns the algorithm with the best time
|
17
|
+
def best(action, data_set, trials=1, target=nil)
|
18
|
+
if action == "sort"
|
19
|
+
Sorts.new.best(data_set, trials)
|
20
|
+
elsif action == "search"
|
21
|
+
Searches.new.best(data_set, trials, target)
|
21
22
|
end
|
22
23
|
end
|
23
24
|
|
24
|
-
#
|
25
|
-
def analyze(
|
26
|
-
if
|
27
|
-
Sorts.new.analyze(
|
28
|
-
elsif
|
29
|
-
Searches.new.analyze(
|
25
|
+
# Returns time for specified algorithm or data structure
|
26
|
+
def analyze(action, data_set, algorithm, trials=1, target=nil)
|
27
|
+
if action == "sort"
|
28
|
+
Sorts.new.analyze(data_set, algorithm, trials)
|
29
|
+
elsif action == "search"
|
30
|
+
Searches.new.analyze(data_set, algorithm, trials, target)
|
30
31
|
end
|
31
32
|
end
|
32
33
|
|
33
|
-
#
|
34
|
-
def compare(
|
35
|
-
if
|
36
|
-
Sorts.new.compare(
|
37
|
-
elsif
|
38
|
-
Searches.new.compare(
|
34
|
+
# Returns the times of two specified algorithms for comparison
|
35
|
+
def compare(action, data_set, first_algorithm, second_algorithm, trials=1, target=nil)
|
36
|
+
if action == "sort"
|
37
|
+
Sorts.new.compare(data_set, first_algorithm, second_algorithm, trials)
|
38
|
+
elsif action == "search"
|
39
|
+
Searches.new.compare(data_set, first_algorithm, second_algorithm, trials, target)
|
39
40
|
end
|
40
41
|
end
|
41
42
|
|
42
|
-
#
|
43
|
-
def sort(
|
44
|
-
method = Sorts.new.method(
|
45
|
-
method.call(
|
43
|
+
# Returns sorted data_set of specified sorting algorithm
|
44
|
+
def sort(data_set, algorithm)
|
45
|
+
method = Sorts.new.method(algorithm)
|
46
|
+
method.call(data_set)
|
46
47
|
end
|
47
48
|
|
48
49
|
end
|
49
50
|
|
50
51
|
# Sorts class
|
52
|
+
# 4 methods corresponding to algorithm_selector module
|
53
|
+
# 5 sorting algorithms (selection, insertion, bubble, merge, quick)
|
51
54
|
class Sorts
|
55
|
+
|
52
56
|
# Show all results from sorting algorithms
|
53
|
-
def all(
|
57
|
+
def all(data_set, trials)
|
54
58
|
{
|
55
|
-
sorted: merge_sort(
|
59
|
+
sorted: merge_sort(data_set),
|
56
60
|
results: {
|
57
|
-
bubble_sort: display_time(Proc.new {bubble_sort(
|
58
|
-
insertion_sort: display_time(Proc.new {insertion_sort(
|
59
|
-
selection_sort: display_time(Proc.new {selection_sort(
|
60
|
-
merge_sort: display_time(Proc.new {merge_sort(
|
61
|
-
quick_sort: display_time(Proc.new {quick_sort(
|
61
|
+
bubble_sort: display_time(Proc.new {bubble_sort(data_set)}, trials),
|
62
|
+
insertion_sort: display_time(Proc.new {insertion_sort(data_set)}, trials),
|
63
|
+
selection_sort: display_time(Proc.new {selection_sort(data_set)}, trials),
|
64
|
+
merge_sort: display_time(Proc.new {merge_sort(data_set)}, trials),
|
65
|
+
quick_sort: display_time(Proc.new {quick_sort(data_set)}, trials)
|
62
66
|
}
|
63
67
|
}
|
64
68
|
end
|
65
69
|
|
66
70
|
# Show the best sorting algorithm
|
67
|
-
def best(
|
68
|
-
hash = all(
|
71
|
+
def best(data_set, trials)
|
72
|
+
hash = all(data_set, trials)
|
69
73
|
best_time = 0
|
70
74
|
best_algorithm = {}
|
71
75
|
hash[:results].each do |key, val|
|
@@ -79,9 +83,9 @@ class Sorts
|
|
79
83
|
}
|
80
84
|
end
|
81
85
|
|
82
|
-
#
|
83
|
-
def analyze(
|
84
|
-
hash = all(
|
86
|
+
# Show specific sorting algorithm by name
|
87
|
+
def analyze(data_set, algorithm, trials)
|
88
|
+
hash = all(data_set, trials)
|
85
89
|
alg = {}
|
86
90
|
hash[:results].each do |key, val|
|
87
91
|
if algorithm.to_sym == key
|
@@ -94,9 +98,9 @@ class Sorts
|
|
94
98
|
}
|
95
99
|
end
|
96
100
|
|
97
|
-
#
|
98
|
-
def compare(
|
99
|
-
hash = all(
|
101
|
+
# Show comparison of two algorithms
|
102
|
+
def compare(data_set, first_algorithm, second_algorithm, trials)
|
103
|
+
hash = all(data_set, trials)
|
100
104
|
first = {}
|
101
105
|
second = {}
|
102
106
|
hash[:results].each do |key, val|
|
@@ -116,69 +120,69 @@ class Sorts
|
|
116
120
|
# Worst-Case Space Complexity: O(1)
|
117
121
|
# Averge-Case Time Complexity: O(n^2)
|
118
122
|
# Worst-Case Time Complexity: O(n^2)
|
119
|
-
def bubble_sort(
|
120
|
-
return
|
123
|
+
def bubble_sort(data_set)
|
124
|
+
return data_set if data_set.length < 2
|
121
125
|
switched = true
|
122
126
|
until switched == false
|
123
127
|
switched = false
|
124
|
-
(1...
|
125
|
-
if
|
126
|
-
temp =
|
127
|
-
|
128
|
-
|
128
|
+
(1...data_set.length).each do |i|
|
129
|
+
if data_set[i-1] > data_set[i]
|
130
|
+
temp = data_set[i]
|
131
|
+
data_set[i] = data_set[i-1]
|
132
|
+
data_set[i-1] = temp
|
129
133
|
switched = true
|
130
134
|
end
|
131
135
|
end
|
132
136
|
end
|
133
|
-
return
|
137
|
+
return data_set
|
134
138
|
end
|
135
139
|
|
136
140
|
# Insertion sort
|
137
141
|
# Worst-Case Space Complexity: O(1)
|
138
142
|
# Averge-Case Time Complexity: O(n^2)
|
139
143
|
# Worst-Case Time Complexity: O(n^2)
|
140
|
-
def insertion_sort(
|
141
|
-
return
|
142
|
-
(1...
|
143
|
-
value =
|
144
|
+
def insertion_sort(data_set)
|
145
|
+
return data_set if data_set.length < 2
|
146
|
+
(1...data_set.length).to_a do |i|
|
147
|
+
value = data_set[i]
|
144
148
|
j = i - 1
|
145
|
-
while (j >= 0 &&
|
146
|
-
|
149
|
+
while (j >= 0 && data_set[j] > value) do
|
150
|
+
data_set[j+1] = data_set[j]
|
147
151
|
j = j - 1
|
148
152
|
end
|
149
|
-
|
153
|
+
data_set[j+1] = value
|
150
154
|
end
|
151
|
-
|
155
|
+
data_set
|
152
156
|
end
|
153
157
|
|
154
158
|
# Selection sort
|
155
159
|
# Worst-Case Space Complexity: O(1)
|
156
160
|
# Averge-Case Time Complexity: O(n^2)
|
157
161
|
# Worst-Case Time Complexity: O(n^2)
|
158
|
-
def selection_sort(
|
159
|
-
(0...
|
162
|
+
def selection_sort(data_set)
|
163
|
+
(0...data_set.length-1).to_a.each do |i|
|
160
164
|
min = i
|
161
|
-
(i+1...
|
162
|
-
if (
|
165
|
+
(i+1...data_set.length).to_a.each do |j|
|
166
|
+
if (data_set[j] < data_set[min])
|
163
167
|
min = j
|
164
168
|
end
|
165
169
|
end
|
166
170
|
if min != i
|
167
|
-
|
171
|
+
data_set[min], data_set[i] = data_set[i], data_set[min]
|
168
172
|
end
|
169
173
|
end
|
170
|
-
return
|
174
|
+
return data_set
|
171
175
|
end
|
172
176
|
|
173
177
|
# Merge sort
|
174
178
|
# Worst-Case Space Complexity: O(n)
|
175
179
|
# Averge-Case Time Complexity: O(n log(n))
|
176
180
|
# Worst-Case Time Complexity: O(n log(n))
|
177
|
-
def merge_sort(
|
178
|
-
return
|
179
|
-
middle =
|
180
|
-
left =
|
181
|
-
right =
|
181
|
+
def merge_sort(data_set)
|
182
|
+
return data_set if data_set.length < 2
|
183
|
+
middle = data_set.length/2
|
184
|
+
left = data_set[0...middle]
|
185
|
+
right = data_set[middle...data_set.length]
|
182
186
|
merge(merge_sort(left), merge_sort(right))
|
183
187
|
end
|
184
188
|
|
@@ -186,35 +190,40 @@ class Sorts
|
|
186
190
|
# Worst-Case Space Complexity: O(log(n))
|
187
191
|
# Averge-Case Time Complexity: O(n log(n))
|
188
192
|
# Worst-Case Time Complexity: O(n^2)
|
189
|
-
def quick_sort(
|
190
|
-
return
|
191
|
-
pivot =
|
192
|
-
left =
|
193
|
-
right =
|
193
|
+
def quick_sort(data_set)
|
194
|
+
return data_set if data_set.count < 2
|
195
|
+
pivot = data_set[0]
|
196
|
+
left = data_set.select {|el| el < pivot}
|
197
|
+
right = data_set.select {|el| el > pivot}
|
194
198
|
quick_sort(left) + [pivot] + quick_sort(right)
|
195
199
|
end
|
196
200
|
end
|
197
201
|
|
202
|
+
# Searches class
|
203
|
+
# 4 methods corresponding to algorithm_selector module
|
198
204
|
class Searches
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
205
|
+
|
206
|
+
# Show all data structures for searching
|
207
|
+
def all(data_set, trials, target)
|
208
|
+
stack = Stack.new(data_set.length, data_set)
|
209
|
+
queue = Queue.new(data_set.length, data_set)
|
210
|
+
linked_list = LinkedList.new(data_set)
|
211
|
+
binary_tree = BinaryTree.new(data_set)
|
204
212
|
{
|
205
213
|
found: stack.search(target),
|
206
214
|
results:
|
207
215
|
{
|
208
|
-
stack: display_time(Proc.new {stack.search(target)},
|
209
|
-
queue: display_time(Proc.new {queue.search(target)},
|
210
|
-
linked_list: display_time(Proc.new {linked_list.search(target)},
|
211
|
-
binary_tree: display_time(Proc.new {binary_tree.search(target)},
|
216
|
+
stack: display_time(Proc.new {stack.search(target)}, trials),
|
217
|
+
queue: display_time(Proc.new {queue.search(target)}, trials),
|
218
|
+
linked_list: display_time(Proc.new {linked_list.search(target)}, trials),
|
219
|
+
binary_tree: display_time(Proc.new {binary_tree.search(target)}, trials)
|
212
220
|
}
|
213
221
|
}
|
214
222
|
end
|
215
223
|
|
216
|
-
|
217
|
-
|
224
|
+
# Show best data structure for searching
|
225
|
+
def best(data_set, trials, target)
|
226
|
+
hash = all(data_set, trials, target)
|
218
227
|
best_time = 0
|
219
228
|
best_algorithm = {}
|
220
229
|
hash[:results].each do |key, val|
|
@@ -228,8 +237,9 @@ class Searches
|
|
228
237
|
}
|
229
238
|
end
|
230
239
|
|
231
|
-
|
232
|
-
|
240
|
+
# Show analysis of a data structure for searching
|
241
|
+
def analyze(data_set, algorithm, trials, target)
|
242
|
+
hash = all(data_set, trials, target)
|
233
243
|
alg = {}
|
234
244
|
hash[:results].each do |key, val|
|
235
245
|
if algorithm.to_sym == key
|
@@ -242,8 +252,9 @@ class Searches
|
|
242
252
|
}
|
243
253
|
end
|
244
254
|
|
245
|
-
|
246
|
-
|
255
|
+
# Show comparison of two data structures for searching
|
256
|
+
def compare(data_set, first_algorithm, second_algorithm, trials, target)
|
257
|
+
hash = all(data_set, trials, target)
|
247
258
|
first = {}
|
248
259
|
second = {}
|
249
260
|
hash[:results].each do |key, val|
|
@@ -263,11 +274,11 @@ end
|
|
263
274
|
# Stack data structure
|
264
275
|
# Worst-Case Space Complexity: O(n)
|
265
276
|
class Stack
|
266
|
-
def initialize(size,
|
277
|
+
def initialize(size, data_set=[])
|
267
278
|
@size = size
|
268
279
|
@store = Array.new(@size)
|
269
280
|
@top = -1
|
270
|
-
|
281
|
+
data_set.each { |el| push(el) }
|
271
282
|
end
|
272
283
|
|
273
284
|
# Search, Average-Case Time Complexity: O(n)
|
@@ -286,7 +297,6 @@ class Stack
|
|
286
297
|
found
|
287
298
|
end
|
288
299
|
|
289
|
-
# Deletion, Worst-Case Time Complexity: O(1)
|
290
300
|
def pop
|
291
301
|
if empty?
|
292
302
|
nil
|
@@ -329,13 +339,13 @@ end
|
|
329
339
|
# Queue data structure
|
330
340
|
# Worst-Case Space Complexity: O(n)
|
331
341
|
class Queue
|
332
|
-
def initialize(size,
|
342
|
+
def initialize(size, data_set=[])
|
333
343
|
@size = size
|
334
344
|
@store = Array.new(@size)
|
335
345
|
@head, @tail = -1, 0
|
336
346
|
|
337
|
-
# initialize Queue with
|
338
|
-
|
347
|
+
# initialize Queue with data_set if given
|
348
|
+
data_set.each { |el| enqueue(el) }
|
339
349
|
end
|
340
350
|
|
341
351
|
# Search, Worst-Case Time Complexity: O(n)
|
@@ -348,7 +358,6 @@ class Queue
|
|
348
358
|
found
|
349
359
|
end
|
350
360
|
|
351
|
-
# Deletion, Worst-Case Time Complexity: O(1)
|
352
361
|
def dequeue
|
353
362
|
if empty?
|
354
363
|
nil
|
@@ -396,12 +405,12 @@ end
|
|
396
405
|
class LinkedList
|
397
406
|
include Enumerable
|
398
407
|
|
399
|
-
def initialize(
|
408
|
+
def initialize(data_set=[])
|
400
409
|
@head = LinkNode.new
|
401
410
|
@tail = LinkNode.new
|
402
411
|
@head.next = @tail
|
403
412
|
@tail.prev = @head
|
404
|
-
|
413
|
+
data_set.each { |i| insert(i) }
|
405
414
|
end
|
406
415
|
|
407
416
|
# Search, Worst-Case Time Complexity: O(n)
|
@@ -473,9 +482,9 @@ end
|
|
473
482
|
class BinaryTree
|
474
483
|
attr_accessor :root
|
475
484
|
|
476
|
-
def initialize(
|
485
|
+
def initialize(data_set=[], root_val=nil)
|
477
486
|
@root = BSTNode.new(root_val)
|
478
|
-
|
487
|
+
data_set.each { |i| insert(i) }
|
479
488
|
end
|
480
489
|
|
481
490
|
# Search, Worst-Case Time Complexity: O(n)
|
@@ -517,28 +526,29 @@ end
|
|
517
526
|
private
|
518
527
|
|
519
528
|
# Helper method to get time required for method to run
|
520
|
-
|
529
|
+
# Returns time in seconds
|
530
|
+
def display_time(prc, trials)
|
521
531
|
total = 0
|
522
|
-
|
532
|
+
trials.times do
|
523
533
|
start = Time.now
|
524
534
|
prc.call
|
525
535
|
finish = Time.now
|
526
536
|
total = total + (finish - start)
|
527
537
|
end
|
528
|
-
total/
|
538
|
+
total/trials
|
529
539
|
end
|
530
540
|
|
531
541
|
# Helper method for merge_sort
|
532
542
|
def merge(left, right)
|
533
|
-
|
543
|
+
data_set = []
|
534
544
|
until left.length == 0 || right.length == 0
|
535
545
|
if left.first > right.first
|
536
|
-
|
546
|
+
data_set << right.shift
|
537
547
|
else
|
538
|
-
|
548
|
+
data_set << left.shift
|
539
549
|
end
|
540
550
|
end
|
541
|
-
|
551
|
+
data_set.concat(left).concat(right)
|
542
552
|
end
|
543
553
|
|
544
554
|
# Node helper class for Linked List
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: algorithm_selector
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- joseph
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-07-
|
11
|
+
date: 2016-07-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -53,8 +53,8 @@ dependencies:
|
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '3.0'
|
55
55
|
description: |-
|
56
|
-
|
57
|
-
Currently supports
|
56
|
+
Calculates algorithms and data structures for sorting and searching. Selects best one to use for particular data set.
|
57
|
+
Currently supports: Bubble Sort, Insertion Sort, Selection Sort, Merge Sort, Quick Sort, Stack, Queue, Linked List, Binary Search Tree
|
58
58
|
email:
|
59
59
|
- jnvbui@gmail.com
|
60
60
|
executables: []
|