algorithm_selector 0.1.3 → 0.1.4

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.
@@ -88,7 +88,7 @@
88
88
 
89
89
 
90
90
 
91
- <strong class="classes">Classes:</strong> <span class='object_link'><a href="BSTNode.html" title="BSTNode (class)">BSTNode</a></span>, <span class='object_link'><a href="BinaryTree.html" title="BinaryTree (class)">BinaryTree</a></span>, <span class='object_link'><a href="LinkNode.html" title="LinkNode (class)">LinkNode</a></span>, <span class='object_link'><a href="LinkedList.html" title="LinkedList (class)">LinkedList</a></span>, <span class='object_link'><a href="Queue.html" title="Queue (class)">Queue</a></span>, <span class='object_link'><a href="Searches.html" title="Searches (class)">Searches</a></span>, <span class='object_link'><a href="Sorts.html" title="Sorts (class)">Sorts</a></span>, <span class='object_link'><a href="Stack.html" title="Stack (class)">Stack</a></span>
91
+ <strong class="classes">Classes:</strong> <span class='object_link'><a href="BSTNode.html" title="BSTNode (class)">BSTNode</a></span>, <span class='object_link'><a href="BinaryMinHeap.html" title="BinaryMinHeap (class)">BinaryMinHeap</a></span>, <span class='object_link'><a href="BinaryTree.html" title="BinaryTree (class)">BinaryTree</a></span>, <span class='object_link'><a href="LinkNode.html" title="LinkNode (class)">LinkNode</a></span>, <span class='object_link'><a href="LinkedList.html" title="LinkedList (class)">LinkedList</a></span>, <span class='object_link'><a href="Queue.html" title="Queue (class)">Queue</a></span>, <span class='object_link'><a href="Searches.html" title="Searches (class)">Searches</a></span>, <span class='object_link'><a href="Sorts.html" title="Sorts (class)">Sorts</a></span>, <span class='object_link'><a href="Stack.html" title="Stack (class)">Stack</a></span>
92
92
 
93
93
 
94
94
  </p>
@@ -104,7 +104,7 @@
104
104
  </div>
105
105
 
106
106
  <div id="footer">
107
- Generated on Sat Jul 23 23:45:47 2016 by
107
+ Generated on Sat Jul 30 20:58:51 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>
Binary file
@@ -62,7 +62,8 @@ class Sorts
62
62
  insertion_sort: display_time(Proc.new {insertion_sort(data_set)}, trials),
63
63
  selection_sort: display_time(Proc.new {selection_sort(data_set)}, trials),
64
64
  merge_sort: display_time(Proc.new {merge_sort(data_set)}, trials),
65
- quick_sort: display_time(Proc.new {quick_sort(data_set)}, trials)
65
+ quick_sort: display_time(Proc.new {quick_sort(data_set)}, trials),
66
+ heap_sort: display_time(Proc.new {heap_sort(data_set)}, trials)
66
67
  }
67
68
  }
68
69
  end
@@ -197,6 +198,21 @@ class Sorts
197
198
  right = data_set.select {|el| el > pivot}
198
199
  quick_sort(left) + [pivot] + quick_sort(right)
199
200
  end
201
+
202
+ # Quick sort
203
+ # Worst-Case Space Complexity: O(1)
204
+ # Averge-Case Time Complexity: O(n log(n))
205
+ # Worst-Case Time Complexity: O(n log(n))
206
+ def heap_sort(data_set)
207
+ (2..data_set.length).to_a.each do |idx|
208
+ BinaryMinHeap.heapify_up(data_set, idx - 1, idx)
209
+ end
210
+ (2..data_set.length).to_a.reverse.each do |idx|
211
+ data_set[idx - 1], data_set[0] = data_set[0], data_set[idx - 1]
212
+ BinaryMinHeap.heapify_down(data_set, 0, idx - 1)
213
+ end
214
+ data_set.reverse!
215
+ end
200
216
  end
201
217
 
202
218
  # Searches class
@@ -570,3 +586,90 @@ class BSTNode
570
586
  @right = right
571
587
  end
572
588
  end
589
+
590
+ # MinHeap helper class for Heap Sort
591
+ class BinaryMinHeap
592
+ def initialize(&prc)
593
+ @store = []
594
+ @prc = prc || Proc.new { |el1, el2| el1 <=> el2 }
595
+ end
596
+
597
+ def count
598
+ @store.length
599
+ end
600
+
601
+ def extract
602
+ raise "no element to extract" if count == 0
603
+ val = @store[0]
604
+ @store.pop if count == 1
605
+ if count > 1
606
+ @store[0] = @store.pop
607
+ self.class.heapify_down(@store, 0, &@prc)
608
+ end
609
+ val
610
+ end
611
+
612
+ def peek
613
+ raise "no element to peek" if count == 0
614
+ @store[0]
615
+ end
616
+
617
+ def push(val)
618
+ @store.push(val)
619
+ self.class.heapify_up(@store, count - 1, &@prc)
620
+ end
621
+
622
+ protected
623
+ attr_accessor :prc, :store
624
+
625
+ public
626
+ def self.child_indices(len, parent_index)
627
+ [2 * parent_index + 1, 2 * parent_index + 2].select { |idx| idx < len }
628
+ end
629
+
630
+ def self.parent_index(child_index)
631
+ raise "root has no parent" if child_index == 0
632
+ (child_index - 1) / 2
633
+ end
634
+
635
+ def self.heapify_down(array, parent_idx, len = array.length, &prc)
636
+ prc ||= Proc.new { |el1, el2| el1 <=> el2 }
637
+
638
+ left_child, right_child = child_indices(len, parent_idx)
639
+
640
+ parent = array[parent_idx]
641
+
642
+ children = []
643
+ children.push(array[left_child]) if left_child
644
+ children.push(array[right_child]) if right_child
645
+
646
+ if children.all? { |child| prc.call(parent, child) <= 0 }
647
+ return array
648
+ end
649
+
650
+ swap_idx = nil
651
+ if children.length == 1
652
+ swap_idx = left_child
653
+ else
654
+ swap_idx =
655
+ prc.call(children[0], children[1]) == -1 ? left_child : right_child
656
+ end
657
+
658
+ array[parent_idx], array[swap_idx] = array[swap_idx], parent
659
+ heapify_down(array, swap_idx, len, &prc)
660
+ end
661
+
662
+ def self.heapify_up(array, child_idx, len = array.length, &prc)
663
+ prc ||= Proc.new { |el1, el2| el1 <=> el2 }
664
+ return array if child_idx == 0
665
+
666
+ parent_idx = parent_index(child_idx)
667
+ child, parent = array[child_idx], array[parent_idx]
668
+ if prc.call(child, parent) >= 0
669
+ return array
670
+ else
671
+ array[child_idx], array[parent_idx] = parent, child
672
+ heapify_up(array, parent_idx, len, &prc)
673
+ end
674
+ end
675
+ end
@@ -1,3 +1,3 @@
1
1
  module AlgorithmSelector
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
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.3
4
+ version: 0.1.4
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-24 00:00:00.000000000 Z
11
+ date: 2016-07-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler