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

// This method uses a heap to sort an array.
// Time Complexity: ?
// Space Complexity: ?
// Time Complexity: O(nlog(n))- both for loops are O(n), heap.add and heap.remove are O(log(n))
// Space Complexity: O(n) - the store array of minHeap has a space complexity of O(n)
function heapsort(list) {
Comment on lines +4 to 6

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...');
let heap = new MinHeap();
const len = list.length;
for (let i = 0; i < len; i++) {
heap.add(list[i]);
}

for (let i = 0; i < len; i++) {
list[i] = heap.remove();
}

return list;
};

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

// This method adds a HeapNode instance to the heap
// Time Complexity: ?
// Space Complexity: ?
// Time Complexity: O(log(n)) - Recursive calls (uses heapUp)
// Space Complexity: O(log(n)) - Recursive calls, log(n) calls in memory (because of heapUp)
add(key, value = key) {
Comment on lines +14 to 16

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 node = new HeapNode(key, value);
const len = this.store.length;
this.store[len] = node;

return this.heapUp(len);
}

// This method removes and returns an element from the heap
// maintaining the heap structure
// Time Complexity: ?
// Space Complexity: ?
// Time Complexity: O(log(n)), recursive (uses heapDown, similar to heapUp)
// Space Complexity: O(log(n)), log(n) recursion calls (uses heapDown)
remove() {
Comment on lines +26 to 28

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 len = this.store.length;
if (this.isEmpty()) {
return undefined;
}
this.swap(0, len - 1);
const removed = this.store.pop();
this.heapDown(0);

return removed.value;
}


Expand All @@ -38,26 +50,77 @@ class MinHeap {
}

// This method returns true if the heap is empty
// Time complexity: ?
// Space complexity: ?
// Time complexity: O(1) - the length property is a quick look up
// Space complexity: O(1) - saving the length of the array will be constant memory
isEmpty() {
Comment on lines +53 to 55

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 len = this.store.length;
if (len === 0) {
return true;
}
return false;
}

// 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)), index is divided by two each time this method is called (n-2)/(2^t) = c (where t is the number of times the method is called and c is a constant (the parent index)). To solve for the number of times this is called, we can simplify this to n = 2^t. Solving for t: t = log2(n).
// Space complexity: O(log(n)), the space complexity here depends on the number of recursion steps, which will be log(n) steps.
heapUp(index) {
throw new Error("Method not implemented yet...");

if (index === 0) {
return;
}

const childNode = this.store[index];
let parentIndex;
if (index % 2 === 0) {
parentIndex = (index - 2) / 2;
} else {
parentIndex = (index - 1) / 2;
}
Comment on lines +76 to +80

Choose a reason for hiding this comment

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

Could you use floor to simplify this a bit?


const parentNode = this.store[parentIndex];

if (childNode.key >= parentNode.key) {
return;
} else {
this.swap(index, parentIndex);
this.heapUp(parentIndex);
}

}

// This helper method takes an index and
// moves it up the heap if it's smaller
// than it's parent node.
heapDown(index) {
Comment on lines 93 to 96

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 leftIndex = (2 * index) + 1;
const len = this.store.length;
const rightIndex = (2 * index) + 2;
const leftNode = this.store[leftIndex];
const rightNode = this.store[rightIndex];
const parentNode = this.store[index];

if (leftIndex >= len) {
return;
} else if (leftIndex === len - 1) {
if (parentNode.key <= leftNode.key) {
return;
} else {
this.swap(index, leftIndex);
return;
}
}

if (parentNode.key <= leftNode.key && parentNode.key <= rightNode.key) {
return;
} else if (leftNode.key < rightNode.key) {
this.swap(index, leftIndex);
this.heapDown(leftIndex);
} else {
this.swap(index, rightIndex);
this.heapDown(rightIndex);
}
}

// If you want a swap method... you're welcome
Expand Down
Loading