compare-sort 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/compare-sort.rb +182 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c91aefa1ba77dbbf0e014fbc3298eee1aa06d4c3
|
4
|
+
data.tar.gz: eb210a91422c7b200339347e0fc90f69b8ce4844
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a4cb697367a54ab5f93d608a39ac13dd167bb809f5007a5e80f42b7294ccb235af527c3ba38121a5f68dc9003dbcfba7fda29c434748524d070eccd37a1da0e8
|
7
|
+
data.tar.gz: 2cd111d1c56dcd942081472f114461cd106fa1b77ef012719d645ae7f33baa0eb489ca7135115d45c251358ae8f406d55f969b75c20fd1d52e411c282b940c8b
|
data/lib/compare-sort.rb
ADDED
@@ -0,0 +1,182 @@
|
|
1
|
+
class CompareSort
|
2
|
+
def self.run(info)
|
3
|
+
data = info[:data]
|
4
|
+
sorting_method = info[:sorting_method]
|
5
|
+
timer = info[:timer]
|
6
|
+
|
7
|
+
ValidateData.run(data)
|
8
|
+
|
9
|
+
if timer
|
10
|
+
sort = lambda { eval(sorting_method).run(data) }
|
11
|
+
return self.timer(sort)
|
12
|
+
else
|
13
|
+
return eval(sorting_method).run(data)
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.timer(sorting_method)
|
19
|
+
start_time = Time.now
|
20
|
+
sorted_list = sorting_method.call
|
21
|
+
end_time = Time.now
|
22
|
+
|
23
|
+
return end_time - start_time
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.compare_all(info)
|
27
|
+
data = info[:data]
|
28
|
+
view = info[:view]
|
29
|
+
|
30
|
+
sorting_methods = %w(SelectionSort BubbleSort ModifiedBubbleSort InsertionSort)
|
31
|
+
sorting_times = {}
|
32
|
+
|
33
|
+
sorting_methods.each do |method|
|
34
|
+
info = { data: data, sorting_method: method, timer: true }
|
35
|
+
sorting_times[method] = self.run(info)
|
36
|
+
end
|
37
|
+
|
38
|
+
if view
|
39
|
+
View.compare_all(sorting_times.sort_by{|method, time| time})
|
40
|
+
end
|
41
|
+
|
42
|
+
return sorting_times
|
43
|
+
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
class View
|
48
|
+
def self.compare_all(data)
|
49
|
+
puts ""
|
50
|
+
print "SORTING METHOD"
|
51
|
+
print " "*(6)
|
52
|
+
puts "SECONDS"
|
53
|
+
puts "-"*27
|
54
|
+
data.each do |datum|
|
55
|
+
print datum[0]
|
56
|
+
print " "*(20-datum[0].length)
|
57
|
+
puts datum[1]
|
58
|
+
end
|
59
|
+
puts ""
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
class ValidateData
|
64
|
+
|
65
|
+
def self.run(data)
|
66
|
+
@data = data
|
67
|
+
self.isArray
|
68
|
+
self.valuesAreConsistent
|
69
|
+
end
|
70
|
+
|
71
|
+
def self.isArray
|
72
|
+
raise "Data must be an array" if (!@data.is_a?(Array))
|
73
|
+
end
|
74
|
+
|
75
|
+
def self.valuesAreConsistent
|
76
|
+
if @data[0].is_a?(String)
|
77
|
+
valuesAreStrings
|
78
|
+
else
|
79
|
+
valuesAreNumbers
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def self.valuesAreNumbers
|
84
|
+
@data.each do |datum|
|
85
|
+
if (!datum.is_a?(Fixnum) && !datum.is_a?(Float))
|
86
|
+
raise "Values in array must be all numbers OR all strings"
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def self.valuesAreStrings
|
92
|
+
@data.each do |datum|
|
93
|
+
if (!datum.is_a?(String))
|
94
|
+
raise "Values in array must be all numbers OR all strings"
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
|
101
|
+
class InsertionSort
|
102
|
+
def self.run(data)
|
103
|
+
|
104
|
+
# iterate through each element
|
105
|
+
data.each_with_index do |unsorted_num, i|
|
106
|
+
data[0..i].each_with_index do |sorted_num, j|
|
107
|
+
|
108
|
+
if sorted_num > unsorted_num
|
109
|
+
# insert to its new spot
|
110
|
+
data.insert(j, unsorted_num)
|
111
|
+
# delete it from old spot
|
112
|
+
data.delete_at(i+1)
|
113
|
+
break
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
117
|
+
end
|
118
|
+
return data
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
|
123
|
+
class SelectionSort
|
124
|
+
def self.run(data)
|
125
|
+
len = data.length
|
126
|
+
|
127
|
+
# iterate through each element in the array
|
128
|
+
for i in 0..len-2
|
129
|
+
min_index = i
|
130
|
+
# iterate through the rest of the array
|
131
|
+
for j in i+1..len-1
|
132
|
+
# if min, save index
|
133
|
+
min_index = j if data[j] < data[min_index]
|
134
|
+
end
|
135
|
+
# put the min in it's correct spot
|
136
|
+
data[i], data[min_index] = data[min_index], data[i]
|
137
|
+
end
|
138
|
+
|
139
|
+
return data
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
|
144
|
+
class ModifiedBubbleSort
|
145
|
+
def self.run(data)
|
146
|
+
sorted = false
|
147
|
+
|
148
|
+
while !sorted
|
149
|
+
sorted = true
|
150
|
+
# iterate through the whole array
|
151
|
+
(data.length - 1).times do |i|
|
152
|
+
# if the element ahead of the one we are on is smaller
|
153
|
+
# then switch them
|
154
|
+
if (data[i] > data[i+1])
|
155
|
+
data[i+1], data[i] = data[i], data[i+1]
|
156
|
+
sorted = false
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
160
|
+
return data
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
class BubbleSort
|
165
|
+
def self.run(data)
|
166
|
+
sorted = false
|
167
|
+
|
168
|
+
(data.length).times do |i|
|
169
|
+
(data.length - 1).times do |j|
|
170
|
+
|
171
|
+
if (data[j] > data[j+1])
|
172
|
+
data[j+1], data[j] = data[j], data[j+1]
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
return data
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
|
182
|
+
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: compare-sort
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Amelia Downs
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-08-14 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A way to compare how long sorting algorithms take on data that you provide
|
14
|
+
email: downs.amelia@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/compare-sort.rb
|
20
|
+
homepage:
|
21
|
+
licenses:
|
22
|
+
- MIT
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirements: []
|
39
|
+
rubyforge_project:
|
40
|
+
rubygems_version: 2.4.1
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: Compare sorting algs
|
44
|
+
test_files: []
|