compare-sort 0.0.0 → 0.1.0

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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/compare-sort.rb +86 -3
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c91aefa1ba77dbbf0e014fbc3298eee1aa06d4c3
4
- data.tar.gz: eb210a91422c7b200339347e0fc90f69b8ce4844
3
+ metadata.gz: a213b50d3034c16f4f65ced7c6ed21bc620a5350
4
+ data.tar.gz: 0b587735f509d537dfcc2ec0f072ae1cd5a08bcf
5
5
  SHA512:
6
- metadata.gz: a4cb697367a54ab5f93d608a39ac13dd167bb809f5007a5e80f42b7294ccb235af527c3ba38121a5f68dc9003dbcfba7fda29c434748524d070eccd37a1da0e8
7
- data.tar.gz: 2cd111d1c56dcd942081472f114461cd106fa1b77ef012719d645ae7f33baa0eb489ca7135115d45c251358ae8f406d55f969b75c20fd1d52e411c282b940c8b
6
+ metadata.gz: c0ef65116f1dd99ec09f83e7a22f22fe79e82fcdec3e2841dd7afa753ceb8363fe73ae31355a94e776ff0115ea2a10c11c1c167a94845b9ff8de3fc14b5a7abc
7
+ data.tar.gz: 6977984061569078024488bd6537880c3bf220b01d2a3cd59b5da83361669cab1d2932e2bb85c8b3e219bb015f3be66a44a94db6ee45805b98b3ca08200ec26a
data/lib/compare-sort.rb CHANGED
@@ -27,12 +27,13 @@ class CompareSort
27
27
  data = info[:data]
28
28
  view = info[:view]
29
29
 
30
- sorting_methods = %w(SelectionSort BubbleSort ModifiedBubbleSort InsertionSort)
30
+ sorting_methods = %w(QuickSort SelectionSort BubbleSort ModifiedBubbleSort InsertionSort MergeSort)
31
31
  sorting_times = {}
32
+ info_hash = { data: data.dup, timer: true }
32
33
 
33
34
  sorting_methods.each do |method|
34
- info = { data: data, sorting_method: method, timer: true }
35
- sorting_times[method] = self.run(info)
35
+ info_hash = { data: data.dup, sorting_method: method, timer: true }
36
+ sorting_times[method] = self.run(info_hash)
36
37
  end
37
38
 
38
39
  if view
@@ -178,5 +179,87 @@ class BubbleSort
178
179
  end
179
180
  end
180
181
 
182
+ class MergeSort
183
+ def self.run(nums)
184
+ return nums if nums == []
185
+ #split into single arrays
186
+ nums.map! {|num| [num]}
187
+
188
+ #run until sorted
189
+ while nums.length != 1
190
+ i = 0
191
+ #iterate through the nested array and merge them
192
+ while i < nums.length
193
+ merged_nums = self.merge(nums[i], nums[i+1])
194
+ nums.delete_at(i+1)
195
+ nums.delete_at(i)
196
+ nums.insert(i, merged_nums)
197
+ i += 1
198
+ end
199
+ end
200
+ return nums[0]
201
+ end
202
+
203
+ def self.merge(nums1, nums2)
204
+ # this will happen if there are an off number of arrays
205
+ return nums1 if !nums2
206
+
207
+ total_length = nums1.length + nums2.length
208
+ sorted_nums = []
209
+
210
+ until sorted_nums.length == total_length
211
+ if nums2.empty?
212
+ sorted_nums += nums1
213
+ elsif nums1.empty?
214
+ sorted_nums += nums2
215
+ elsif nums2[0] < nums1[0]
216
+ sorted_nums << nums2.shift
217
+ else
218
+ sorted_nums << nums1.shift
219
+ end
220
+ end
221
+ return sorted_nums
222
+ end
223
+ end
224
+
225
+
226
+ class QuickSort
227
+ def self.run(data)
228
+ return data if data.length <= 1
229
+
230
+ return self.sort_section(data, 0, data.length-1)
231
+ end
232
+
233
+ def self.sort_section(data, start_loc, end_loc)
234
+ return data if end_loc - start_loc <1
235
+ wall = start_loc
236
+ pivot = data[end_loc]
237
+
238
+
239
+ for i in start_loc..end_loc #valid indicies
240
+ if data[i]<pivot
241
+ smaller_datum = data[i]
242
+ data.delete_at(i)
243
+ data.insert(wall, smaller_datum)
244
+ wall += 1
245
+ end
246
+ end
247
+ data.insert(wall, pivot)
248
+ data.delete_at(end_loc + 1)
249
+
250
+ self.sort_section(data, start_loc, wall-1)
251
+ self.sort_section(data, wall+1, end_loc)
252
+ end
253
+ end
254
+
255
+
256
+
257
+
258
+
259
+
260
+
261
+
262
+
263
+
181
264
 
182
265
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: compare-sort
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Amelia Downs