compare-sort 0.1.0 → 0.1.1

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 +126 -1
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a213b50d3034c16f4f65ced7c6ed21bc620a5350
4
- data.tar.gz: 0b587735f509d537dfcc2ec0f072ae1cd5a08bcf
3
+ metadata.gz: d99dd7e85db5eb562c91e9b7304598db32f5460f
4
+ data.tar.gz: f64cf9561dad9535f2e96d5548dd8d74f7324069
5
5
  SHA512:
6
- metadata.gz: c0ef65116f1dd99ec09f83e7a22f22fe79e82fcdec3e2841dd7afa753ceb8363fe73ae31355a94e776ff0115ea2a10c11c1c167a94845b9ff8de3fc14b5a7abc
7
- data.tar.gz: 6977984061569078024488bd6537880c3bf220b01d2a3cd59b5da83361669cab1d2932e2bb85c8b3e219bb015f3be66a44a94db6ee45805b98b3ca08200ec26a
6
+ metadata.gz: f0323d440e2ae1114d746bfa9a7934434bd53dddb240d8758bd11ca747668db44730ffce19cd5094a778fde8ab4442a2fc3a0dbad7597241bbf93306b4aa3579
7
+ data.tar.gz: 11c35e1f3564da55da5b8ba7708fd5cb5ab0853959169a3c723901e0091123f396e2589415d930696d16fc29ba5260b4ca1673008aa7a50764785ba7d408c289
@@ -27,7 +27,7 @@ class CompareSort
27
27
  data = info[:data]
28
28
  view = info[:view]
29
29
 
30
- sorting_methods = %w(QuickSort SelectionSort BubbleSort ModifiedBubbleSort InsertionSort MergeSort)
30
+ sorting_methods = %w(HeapSort QuickSort SelectionSort BubbleSort ModifiedBubbleSort InsertionSort MergeSort)
31
31
  sorting_times = {}
32
32
  info_hash = { data: data.dup, timer: true }
33
33
 
@@ -252,7 +252,132 @@ class QuickSort
252
252
  end
253
253
  end
254
254
 
255
+ class HeapSort
256
+ def self.run(data)
257
+ return data if data.length <= 1
258
+
259
+ # create heap
260
+ heap = self.create_heap(data)
261
+
262
+ # then sort
263
+ sorted_data = self.sort_heap(heap)
264
+
265
+ return sorted_data
266
+ end
267
+
268
+ def self.create_heap(data)
269
+ for i in 0..(data.length-1)
270
+ # if they are greater than their parent swap them
271
+ current_index = i
272
+ current_value = data[i]
273
+ parent_index = get_parent(i)
274
+
275
+ while parent_index != nil && current_value > data[parent_index]
276
+ parent_value = data[parent_index]
277
+
278
+ # swap
279
+ data[parent_index] = current_value
280
+ data[current_index] = parent_value
281
+
282
+ #reset values
283
+ current_index = parent_index
284
+ parent_index = get_parent(current_index)
285
+
286
+ end
287
+ end
288
+ return data
289
+ end
290
+
291
+ def self.get_parent(index)
292
+ parent_index = (index-1)/2
293
+ return nil if parent_index < 0
294
+ return parent_index
295
+ end
296
+
297
+ def self.sort_heap(heap)
298
+ number_sorted = 0
299
+
300
+ while number_sorted < heap.length
301
+ # switch the root and the last element
302
+ # puts "swapping root: #{heap[0]} with bottom: #{heap[-1-number_sorted]}"
303
+
304
+ heap[0], heap[-1-number_sorted] = heap[-1-number_sorted], heap[0]
305
+ # p heap
306
+
307
+ number_sorted += 1
308
+ current_index = 0
309
+ current_value = heap[0]
310
+
311
+ while !self.valid_parent(current_index, heap, number_sorted)
312
+
313
+ # find highest valid child index
314
+ max_child_i = self.find_larger_child(current_index, heap, number_sorted)
315
+
316
+ # swap them
317
+
318
+ heap[max_child_i], heap[current_index] = heap[current_index], heap[max_child_i]
319
+
320
+ # reset values
321
+ current_index = max_child_i
322
+
323
+ end
324
+ end
325
+ return heap
326
+
327
+ end
328
+
329
+ def self.find_larger_child(parent_index, heap, number_sorted)
330
+ children_i = []
331
+
332
+ l_child_i = left_child_index(parent_index)
333
+ r_child_i = right_child_index(parent_index)
334
+
335
+ if l_child_i < (heap.length - number_sorted)
336
+ children_i << l_child_i
337
+ end
338
+
339
+ if r_child_i < (heap.length - number_sorted)
340
+ children_i << r_child_i
341
+ end
342
+
343
+ return nil if children_i.length == 0
344
+
345
+ max_child_i = children_i[0]
346
+
347
+ children_i.each do |index|
348
+ max_child_i = index if heap[index] > heap[max_child_i]
349
+ end
350
+
351
+ return max_child_i
352
+
353
+ end
255
354
 
355
+ def self.valid_parent(parent_index, heap, number_sorted)
356
+ parent_value = heap[parent_index]
357
+
358
+ valid = true
359
+ l_child_i = left_child_index(parent_index)
360
+ r_child_i = right_child_index(parent_index)
361
+
362
+ if l_child_i < (heap.length - number_sorted) && heap[l_child_i] > heap[parent_index]
363
+ valid = false
364
+ end
365
+
366
+ if r_child_i < (heap.length - number_sorted) && heap[r_child_i] > heap[parent_index]
367
+ valid = false
368
+ end
369
+
370
+ return valid
371
+ end
372
+
373
+ def self.left_child_index(parent_index)
374
+ return 2*parent_index + 1
375
+ end
376
+
377
+ def self.right_child_index(parent_index)
378
+ return 2*parent_index + 2
379
+ end
380
+ end
256
381
 
257
382
 
258
383
 
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.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Amelia Downs