Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
11 changes: 5 additions & 6 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# frozen_string_literal: true
source "https://rubygems.org"

gem 'rake'
gem 'minitest'
gem 'minitest-spec'
gem 'minitest-reporters'
gem "rake"
gem "minitest"
gem "minitest-spec"
gem "minitest-reporters"
gem "pry"
gem 'minitest-skip'

gem "minitest-skip"
4 changes: 2 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require 'rake/testtask'
require "rake/testtask"

Rake::TestTask.new do |t|
t.libs = ["lib"]
t.warning = true
t.test_files = FileList['test/*_test.rb']
t.test_files = FileList["test/*_test.rb"]
end

task default: :test
21 changes: 15 additions & 6 deletions lib/heap_sort.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@

require_relative "./min_heap"

# This method uses a heap to sort an array.
# Time Complexity: ?
# Space Complexity: ?
def heap_sort(list)
raise NotImplementedError, "Method not implemented yet..."
end
# Time Complexity: O(long n)
# Space Complexity: O(n)
def heapsort(list)
min_heap = MinHeap.new()
list.each do |item|
min_heap.add(item)
end
empty_arr = []

list.length.times do
empty_arr << min_heap.remove()
end
return empty_arr
end
75 changes: 59 additions & 16 deletions lib/min_heap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,56 @@ def initialize(key, value)
end

class MinHeap

def initialize
@store = []
end

# This method adds a HeapNode instance to the heap
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O (log n)
# Space Complexity: O (n)
def add(key, value = key)
raise NotImplementedError, "Method not implemented yet..."
heap_node = HeapNode.new(key, value)
@store.push(heap_node)
curent_index = @store.length - 1
parent_index = (curent_index - 1) / 2
while heap_node.key < @store[parent_index].key
swap(curent_index, parent_index)
curent_index = parent_index
parent_index = (curent_index - 1) / 2
break if parent_index < 0
end
end

# This method removes and returns an element from the heap
# maintaining the heap structure
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O (log n)
# Space Complexity: O (n)
def remove()
raise NotImplementedError, "Method not implemented yet..."
return nil if @store.empty?
swap(0, @store.length - 1)
result = @store.pop
curent_index = 0
left_child_index = 1
right_child_index = 2
while @store[left_child_index] || @store[right_child_index]
if @store[right_child_index] && @store[right_child_index].key < @store[left_child_index].key && @store[right_child_index].key < @store[curent_index].key
swap(curent_index, right_child_index)
curent_index = right_child_index
elsif @store[right_child_index] && @store[right_child_index].key > @store[left_child_index].key && @store[left_child_index].key < @store[curent_index].key
swap(curent_index, left_child_index)
curent_index = left_child_index
elsif @store[left_child_index] && @store[left_child_index].key < @store[curent_index].key
swap(curent_index, left_child_index)
curent_index = left_child_index
else
break
end
left_child_index = (curent_index * 2) + 1
right_child_index = (curent_index * 2) + 2
end
return result.value
end


# Used for Testing
def to_s
return "[]" if @store.empty?
Expand All @@ -39,15 +68,15 @@ def to_s
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..."
return @store.empty?
end

private
Expand All @@ -58,14 +87,28 @@ def empty?
# Time complexity: ?
# Space complexity: ?
def heap_up(index)

return nil if @store.empty?
parent_index = (index - 1)/2
if @store[index].key >= @store[parent_index].key || index == 0
return @store
else
swap(index, parent_index)
heap_up(parent_index)
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..."
min = index
left, right = 2 * index + 1, 2 * index + 2
left < @store.length && @store[left].key < @store[min].key ? min = left : nil
right < @store.length && @store[right].key < @store[min].key ? min = right : nil
if min != index
swap(index, min)
heap_down(min)
end
end

# If you want a swap method... you're welcome
Expand All @@ -74,4 +117,4 @@ def swap(index_1, index_2)
@store[index_1] = @store[index_2]
@store[index_2] = temp
end
end
end
14 changes: 7 additions & 7 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
end
44 changes: 22 additions & 22 deletions test/min_heap_test.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require_relative "test_helper"

describe "Heap" do
let(:heap) {MinHeap.new}
let(:heap) { MinHeap.new }
it "can be created" do

# Assert
expect(heap).must_be_instance_of MinHeap
end
Expand Down Expand Up @@ -52,30 +52,30 @@
end

it "can remove nodes in the proper order" do
# Arrange
heap.add(3, "Pasta")
heap.add(6, "Soup")
heap.add(1, "Pizza")
heap.add(0, "Donuts")
heap.add(16, "Cookies")
heap.add(57, "Cake")
# Arrange
heap.add(3, "Pasta")
heap.add(6, "Soup")
heap.add(1, "Pizza")
heap.add(0, "Donuts")
heap.add(16, "Cookies")
heap.add(57, "Cake")

# Act
removed = heap.remove
# Act
removed = heap.remove

# Assert
expect(removed).must_equal "Donuts"
# Assert
expect(removed).must_equal "Donuts"

# Another Act
removed = heap.remove
# Another Act
removed = heap.remove

# Another assert
expect(removed).must_equal "Pizza"
# Another assert
expect(removed).must_equal "Pizza"

# Another Act
removed = heap.remove
# Another Act
removed = heap.remove

# Another assert
expect(removed).must_equal "Pasta"
# Another assert
expect(removed).must_equal "Pasta"
end
end
end
10 changes: 5 additions & 5 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require 'minitest'
require 'minitest/autorun'
require 'minitest/reporters'
require "minitest"
require "minitest/autorun"
require "minitest/reporters"
require "minitest/skip_dsl"

Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new

require_relative '../lib/min_heap'
require_relative '../lib/heap_sort'
require_relative "../lib/min_heap"
require_relative "../lib/heap_sort"