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.
@@ -104,7 +104,7 @@
104
104
  </div>
105
105
 
106
106
  <div id="footer">
107
- Generated on Fri Jul 22 11:22:30 2016 by
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>
@@ -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
- # get all algorithms
7
- def all(type, array, num_tests=1, target=nil)
8
- if type == "sort"
9
- Sorts.new.all(array, num_tests)
10
- elsif type == "search"
11
- Searches.new.all(array, num_tests, target)
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
- # get best algorithm
16
- def best(type, array, num_tests=1, target=nil)
17
- if type == "sort"
18
- Sorts.new.best(array, num_tests)
19
- elsif type == "search"
20
- Searches.new.best(array, num_tests, target)
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
- # get one algorithm by name
25
- def analyze(type, array, algorithm_name, num_tests=1, target=nil)
26
- if type == "sort"
27
- Sorts.new.analyze(array, algorithm_name, num_tests)
28
- elsif type == "search"
29
- Searches.new.analyze(array, algorithm_name, num_tests, target)
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
- # compare two algorithms
34
- def compare(type, array, first_algorithm_name, second_algorithm_name, num_tests=1, target=nil)
35
- if type == "sort"
36
- Sorts.new.compare(array, first_algorithm_name, second_algorithm_name, num_tests)
37
- elsif type == "search"
38
- Searches.new.compare(array, first_algorithm_name, second_algorithm_name, num_tests, target)
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
- # call sort algorithm method
43
- def sort(array, algorithm_name)
44
- method = Sorts.new.method(algorithm_name)
45
- method.call(array)
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(array, num_tests)
57
+ def all(data_set, trials)
54
58
  {
55
- sorted: merge_sort(array),
59
+ sorted: merge_sort(data_set),
56
60
  results: {
57
- bubble_sort: display_time(Proc.new {bubble_sort(array)}, num_tests),
58
- insertion_sort: display_time(Proc.new {insertion_sort(array)}, num_tests),
59
- selection_sort: display_time(Proc.new {selection_sort(array)}, num_tests),
60
- merge_sort: display_time(Proc.new {merge_sort(array)}, num_tests),
61
- quick_sort: display_time(Proc.new {quick_sort(array)}, num_tests)
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(array, num_tests)
68
- hash = all(array, num_tests)
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
- # Analyze specific sorting algorithm by name
83
- def analyze(array, algorithm, num_tests)
84
- hash = all(array, num_tests)
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
- # Compare two algorithms for data set
98
- def compare(array, first_algorithm, second_algorithm, num_tests)
99
- hash = all(array, num_tests)
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(array)
120
- return array if array.length < 2
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...array.length).each do |i|
125
- if array[i-1] > array[i]
126
- temp = array[i]
127
- array[i] = array[i-1]
128
- array[i-1] = temp
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 array
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(array)
141
- return array if array.length < 2
142
- (1...array.length).to_a do |i|
143
- value = array[i]
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 && array[j] > value) do
146
- array[j+1] = array[j]
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
- array[j+1] = value
153
+ data_set[j+1] = value
150
154
  end
151
- array
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(array)
159
- (0...array.length-1).to_a.each do |i|
162
+ def selection_sort(data_set)
163
+ (0...data_set.length-1).to_a.each do |i|
160
164
  min = i
161
- (i+1...array.length).to_a.each do |j|
162
- if (array[j] < array[min])
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
- array[min], array[i] = array[i], array[min]
171
+ data_set[min], data_set[i] = data_set[i], data_set[min]
168
172
  end
169
173
  end
170
- return array
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(array)
178
- return array if array.length < 2
179
- middle = array.length/2
180
- left = array[0...middle]
181
- right = array[middle...array.length]
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(array)
190
- return array if array.count < 2
191
- pivot = array[0]
192
- left = array.select {|el| el < pivot}
193
- right = array.select {|el| el > pivot}
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
- def all(array, num_tests, target)
200
- stack = Stack.new(array.length, array)
201
- queue = Queue.new(array.length, array)
202
- linked_list = LinkedList.new(array)
203
- binary_tree = BinaryTree.new(array)
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)}, num_tests),
209
- queue: display_time(Proc.new {queue.search(target)}, num_tests),
210
- linked_list: display_time(Proc.new {linked_list.search(target)}, num_tests),
211
- binary_tree: display_time(Proc.new {binary_tree.search(target)}, num_tests)
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
- def best(array, num_tests, target)
217
- hash = all(array, num_tests, target)
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
- def analyze(array, algorithm, num_tests, target)
232
- hash = all(array, num_tests, target)
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
- def compare(array, first_algorithm, second_algorithm, num_tests, target)
246
- hash = all(array, num_tests, target)
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, array=[])
277
+ def initialize(size, data_set=[])
267
278
  @size = size
268
279
  @store = Array.new(@size)
269
280
  @top = -1
270
- array.each { |el| push(el) }
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, array=[])
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 array if given
338
- array.each { |el| enqueue(el) }
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(array=[])
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
- array.each { |i| insert(i) }
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(array=[], root_val=nil)
485
+ def initialize(data_set=[], root_val=nil)
477
486
  @root = BSTNode.new(root_val)
478
- array.each { |i| insert(i) }
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
- def display_time(prc, num_tests)
529
+ # Returns time in seconds
530
+ def display_time(prc, trials)
521
531
  total = 0
522
- num_tests.times do
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/num_tests
538
+ total/trials
529
539
  end
530
540
 
531
541
  # Helper method for merge_sort
532
542
  def merge(left, right)
533
- array = []
543
+ data_set = []
534
544
  until left.length == 0 || right.length == 0
535
545
  if left.first > right.first
536
- array << right.shift
546
+ data_set << right.shift
537
547
  else
538
- array << left.shift
548
+ data_set << left.shift
539
549
  end
540
550
  end
541
- array.concat(left).concat(right)
551
+ data_set.concat(left).concat(right)
542
552
  end
543
553
 
544
554
  # Node helper class for Linked List
@@ -1,3 +1,3 @@
1
1
  module AlgorithmSelector
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
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.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-22 00:00:00.000000000 Z
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
- A gem for determing the most efficient algorithm or data structure to use for a particular data set.
57
- Currently supports the following data structures and algorithms: Bubble Sort, Insertion Sort, Selection Sort, Merge Sort, Quick Sort, Stack, Queue, Linked List, Binary Search Tree
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: []