Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Janice H. #20

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 64 additions & 4 deletions lib/heap_sort.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,68 @@


# This method uses a heap to sort an array.
# Time Complexity: ?
# Space Complexity: ?
def heap_sort(list)
raise NotImplementedError, "Method not implemented yet..."
# Time Complexity: O(2nlogn)?
# Space Complexity: O(1)
# GAH I cannot get those last two numbers
# sorted properly for some reason and it's
# time to turn this in. I know they are in
# the correct order at the end but then flip
def heapsort(list)
Comment on lines +4 to +10

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's neat that you were trying to do the in-place heapsort. I'd suggest doing it the simple way first, using the minheap you created in this assignment and adding all the elements of the list to the heap and then extracting them one by one and shoveling them into the resulting list. It's O(n) space complexity but it works. Then try for this one if/when you have time.

return [] if list == []
return list if list.length == 1
index = 1
until index == list.length
heap_up(list, index)
index += 1
end
find_min(list)
return list
end

def heap_up(list, index)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To do this in-place, I would suggest making a Max_heap instead of a min-heap and then as you remove a node adding it to the rear of the array working your way from the last node until index 0. That's a lot easier than using a min-heap like you are doing.

# for right children
if index % 2 == 0
parent = (index - 2) / 2
unless parent < 0
if list[index] < list[parent]
swap(list, index, parent)
print list
heap_up(list, parent)
end
end
end
# for left children
if index % 2 == 1
parent = (index - 1) / 2
unless parent < 0
if list[index] < list[parent]
swap(list, index, parent)
print list
heap_up(list, parent)
end
end
end
end

def swap(list, index_1, index_2)
temp = list[index_1]
list[index_1] = list[index_2]
list[index_2] = temp
end

def find_min(list)
return list[0..1] if list.length == 2
heap_down(list, 1)
print list
return find_min(list[1...list.length])
end

def heap_down(list, index)
if !list[index+1].nil? && list[index] > list[index+1]
swap(list, index, index+1)
heap_down(list, index+1)
elsif !list[index+2].nil? && list[index] > list[index+2]
swap(list, index, index+2)
heap_down(list, index+2)
end
end
93 changes: 65 additions & 28 deletions lib/min_heap.rb
Original file line number Diff line number Diff line change
@@ -1,73 +1,110 @@
class HeapNode
attr_reader :key, :value

def initialize(key, value)
@key = key
@value = value
end
end

class MinHeap

def initialize
@store = []
end

# This method adds a HeapNode instance to the heap
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(nlogn)
# Space Complexity: O(n)
def add(key, value = key)
Comment on lines +17 to 19

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works, but why would adding one value to the heap be O(n log n) time complexity and why would it take O(n) space complexity?

I suggest it's O(log n) for space (the recursive stack) and O(log n) for time.

raise NotImplementedError, "Method not implemented yet..."
new_node = HeapNode.new(key, value)
@store << new_node
heap_up(@store.length-1)
end

# This method removes and returns an element from the heap
# maintaining the heap structure
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(nlogn)
# Space Complexity: O(1)
def remove()
Comment on lines +27 to 29

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my notes above about space/time complexity.

raise NotImplementedError, "Method not implemented yet..."
if @store.empty?
return nil
end
swap(0, @store.length-1)
min = @store.pop
heap_down(0) unless @store.empty?
return min.value
end


# Used for Testing
def to_s
return "[]" if @store.empty?

output = "["
(@store.length - 1).times do |index|
output += @store[index].value + ", "
end

output += @store.last.value + "]"

return output
end

# This method returns true if the heap is empty
# Time complexity: ?
# Space complexity: ?
# Time complexity: O(1)
# Space complexity: O(1)
def empty?
raise NotImplementedError, "Method not implemented yet..."
if @store == []
return true
else
return false
end
jaitch marked this conversation as resolved.
Show resolved Hide resolved
end

private

# This helper method takes an index and
# moves it up the heap, if it is less than it's parent node.
# It could be **very** helpful for the add method.
# Time complexity: ?
# Space complexity: ?
# Time complexity: O(nlogn)
# Space complexity: O(1)
def heap_up(index)
Comment on lines +66 to 68

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is the time complexity to heap up 1 element O(n log n), you don't, or shouldn't look at every node in the list for this method.


# for right children
if index % 2 == 0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These if statements could be condensed a bit.

parent = (index - 2) / 2
unless parent < 0
if @store[index].key < @store[parent].key
swap(index, parent)
heap_up(parent)
end
end
end
# for left children
if index % 2 == 1
parent = (index - 1) / 2
unless parent < 0
if @store[index].key < @store[parent].key
swap(index, parent)
heap_up(parent)
end
end
end
end

# This helper method takes an index and
# This helper method takes an index and
# moves it up the heap if it's smaller
# than it's parent node.
def heap_down(index)
raise NotImplementedError, "Method not implemented yet..."
if !@store[index+1].nil? && @store[index].key > @store[index+1].key
swap(index, index+1)
heap_down(index+1)
elsif !@store[index+2].nil? && @store[index].key > @store[index+2].key
swap(index, index+2)
heap_down(index+2)
end
end
Comment on lines 94 to 102

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is NOT doing head_down. Instead it's comparing a node to the nodes next to it. Remember the formula to find a node's left child is:

left_child_index = index * 2 + 1
and the right child
right_child_index = index * 2 + 2

Because you're not doing the correct heap_down method this method is just using a sorted array which is O(n)


# If you want a swap method... you're welcome
def swap(index_1, index_2)
temp = @store[index_1]
Expand Down
12 changes: 6 additions & 6 deletions test/heapsort_test.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require_relative "test_helper"

xdescribe "heapsort" do
describe "heapsort" do
it "sorts an empty array" do
# Arrange
# Arrange
list = []

# Act
Expand All @@ -13,7 +13,7 @@
end

it "can sort a 1-element array" do
# Arrange
# Arrange
list = [5]

# Act
Expand All @@ -22,15 +22,15 @@
# Assert
expect(result).must_equal [5]
end

it "can sort a 5-element array" do
# Arrange
# Arrange
list = [5, 27, 3, 16, -50]

# Act
result = heapsort(list)

# Assert
expect(result).must_equal [-50, 3, 5, 16, 27]
end
end
end