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
18 changes: 14 additions & 4 deletions lib/heapsort.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
const { MinHeap } = require('./minheap');

// This method uses a heap to sort an array.
// Time Complexity: ?
// Space Complexity: ?
// Time Complexity: O(n log n), where n is the number of elements to be sorted
// Space Complexity: O(n), where n is the number of elements to be sorted
function heapsort(list) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍

throw new Error('Method not implemented yet...');
};
const heap = new MinHeap;
list.forEach((item) => {
heap.add(item);
});
const results = [];
for(let i = 0; i < list.length; i += 1) {
results.push(heap.remove());
}
return results;
}

module.exports = heapsort;
61 changes: 46 additions & 15 deletions lib/minheap.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,27 @@ class MinHeap {
}

// This method adds a HeapNode instance to the heap
// Time Complexity: ?
// Space Complexity: ?
// Time Complexity: O(log n), where n is the number of nodes in the heap
// Space Complexity: O(1)
add(key, value = key) {
throw new Error("Method not implemented yet...");
const newNode = new HeapNode(key, value);
this.store.push(newNode);
this.heapUp(this.store.length - 1);
return this.store;
}

// This method removes and returns an element from the heap
// maintaining the heap structure
// Time Complexity: ?
// Space Complexity: ?
// Time Complexity: O(log n), where n is the number of nodes in the heap
// Space Complexity: O(1)
remove() {
throw new Error("Method not implemented yet...");
if (this.store.length === 0) {
return undefined;
}
this.swap(0, this.store.length - 1);
const removed = this.store.pop();
this.heapDown(0);
return removed.value;
}


Expand All @@ -38,26 +47,48 @@ class MinHeap {
}

// This method returns true if the heap is empty
// Time complexity: ?
// Space complexity: ?
// Time complexity: O(1)
// Space complexity: O(1)
isEmpty() {
throw new Error("Method not implemented yet...");
return this.store[0];
}

// 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(log n), where n is the number of nodes in the heap
// Space complexity: O(1)
heapUp(index) {
throw new Error("Method not implemented yet...");
const parentNodeIndex = Math.floor((index - 1) / 2);
if (index === 0 || (this.store[index].key >= this.store[parentNodeIndex].key)) {
return;
}
this.swap(index, parentNodeIndex);
this.heapUp(parentNodeIndex);
}

// This helper method takes an index and
// moves it up the heap if it's smaller
// than it's parent node.
// moves it down the heap if it's larger
// than its children
heapDown(index) {
throw new Error("Method not implemented yet...");
const leftChild = index * 2 + 1;
const rightChild = index * 2 + 2;
let smaller;
if (rightChild >= this.store.length) {
if (leftChild >= this.store.length) {
return;
}
smaller = leftChild;
} else {
smaller = this.store[leftChild].key <= this.store[rightChild].key ? leftChild : rightChild;
}

if (this.store[index].key <= this.store[smaller].key) {
return;
}

this.swap(index, smaller);
this.heapDown(smaller);
}

// If you want a swap method... you're welcome
Expand Down
3 changes: 2 additions & 1 deletion test/heapsort.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const expect = require('chai').expect;
const heapsort = require('../lib/heapsort');

describe.skip("heapsort", function() {
describe("heapsort", function() {
it("sorts an empty array", function() {
// Arrange
const list = [];
Expand Down