This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'pry' | |
puts "Heap Sort" | |
# 1. In def heap_sort, we first build a max heap. | |
# 2. Then, we call a loop: while the array length is greater than 1, swap root element with the last element, reduce the length by 1 (since we have sorted one item), and re-build the heap so that it satisfies the max-heap condition. | |
# 3. At the end, we remake the array into its original size and revert the root back to index [0]. | |
# 4. The def heapify method will sift the elements until they’re in their rightful place. | |
# 5. parent indicates where to start sifting and limit tells how far down the heap to sift. | |
# 6. Set parent node as the root. While the child index is less than or equal to the limit (indicates that root has at least one child), we increment child by 1 to get it’s sibling IF the child is less than limit and the value of child is smaller than value of it’s sibling. The loop terminates if value of root is greater than value of child (since root must always be greater than children in a max heap). Otherwise, swap the |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
puts "Pride and Prejudice Logic" | |
# It is a truth universally acknowledged, that a single man in possession of a good fortune, must be in want of a wife. | |
class Man | |
attr_reader :truth | |
def initialize (name, single, fortune) | |
@fortune = fortune | |
@single = single |
OlderNewer