-
Notifications
You must be signed in to change notification settings - Fork 7
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
C16 - Afina Walton #1
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,21 @@ | ||
const { MinHeap } = require('./minheap'); | ||
// This method uses a heap to sort an array. | ||
// Time Complexity: ? | ||
// Space Complexity: ? | ||
// Time Complexity: O(n log n) | ||
// Space Complexity: O(n) | ||
function heapsort(list) { | ||
throw new Error('Method not implemented yet...'); | ||
if (list.length === 0) return list; | ||
|
||
const heap = new MinHeap(); | ||
|
||
for (const num of list) { | ||
heap.add(num); | ||
} | ||
|
||
for (let i = 0; i < list.length; i++) { | ||
list[i] = heap.remove(); | ||
} | ||
|
||
return list; | ||
}; | ||
|
||
module.exports = heapsort; |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -11,18 +11,36 @@ class MinHeap { | |||||
} | ||||||
|
||||||
// This method adds a HeapNode instance to the heap | ||||||
// Time Complexity: ? | ||||||
// Space Complexity: ? | ||||||
// Time Complexity: O(log n) | ||||||
// Space Complexity: O(1) | ||||||
add(key, value = key) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✨ However space complexity will be O(log n) here because of the recursive call stack of |
||||||
throw new Error("Method not implemented yet..."); | ||||||
// create new HeapNode | ||||||
const newNode = new HeapNode(key, value); | ||||||
|
||||||
// add the node to the end | ||||||
this.store.push(newNode); | ||||||
|
||||||
// perform heap-up where parent must be less than children | ||||||
const index = this.store.length - 1; | ||||||
this.heapUp(index); | ||||||
} | ||||||
|
||||||
// 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(1) | ||||||
remove() { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✨ However space complexity will be O(log n) here because of the recursive call stack of |
||||||
throw new Error("Method not implemented yet..."); | ||||||
// if MinHeap is empty | ||||||
if (this.store.length === 0) return; | ||||||
|
||||||
// if MinHeap is not empty | ||||||
this.swap(0, this.store.length - 1); | ||||||
// remove last | ||||||
const removed = this.store.pop(); | ||||||
// take the root and do a heap down | ||||||
this.heapDown(0); | ||||||
|
||||||
return removed.value; | ||||||
} | ||||||
|
||||||
|
||||||
|
@@ -38,26 +56,72 @@ class MinHeap { | |||||
} | ||||||
|
||||||
// This method returns true if the heap is empty | ||||||
// Time complexity: ? | ||||||
// Space complexity: ? | ||||||
// Time complexity: O(1) | ||||||
// Space complexity: O(1) | ||||||
isEmpty() { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✨ |
||||||
throw new Error("Method not implemented yet..."); | ||||||
if (this.store.length === 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) | ||||||
// Space complexity: O(1) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✨ However, because of the recursive call stack space complexity will be O(log n) |
||||||
heapUp(index) { | ||||||
throw new Error("Method not implemented yet..."); | ||||||
// update position in heap/array | ||||||
const parentIndex = Math.floor((index - 1) / 2); | ||||||
|
||||||
const element = this.store[index]; | ||||||
const parent = this.store[parentIndex]; | ||||||
|
||||||
// base case | ||||||
if (index === 0 || parent.key < element.key) { | ||||||
// pass | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This works however, it's generally considered bad style to leave the body of an if statement empty
Suggested change
|
||||||
} else if (parent.key > element.key) { | ||||||
this.swap(parentIndex, index); | ||||||
index = parentIndex; | ||||||
this.heapUp(index); | ||||||
} | ||||||
|
||||||
return; | ||||||
} | ||||||
|
||||||
// This helper method takes an index and | ||||||
// moves it up the heap if it's smaller | ||||||
// than it's parent node. | ||||||
heapDown(index) { | ||||||
throw new Error("Method not implemented yet..."); | ||||||
const element = this.store[index]; | ||||||
|
||||||
const rightChildIndex = (index * 2) + 2; | ||||||
const leftChildIndex = (index * 2) + 1; | ||||||
let leftChild, rightChild; | ||||||
let smallestItemIndex = index; | ||||||
|
||||||
if (leftChildIndex < this.store.length) { | ||||||
leftChild = this.store[leftChildIndex]; | ||||||
if (leftChild.key < element.key) { | ||||||
smallestItemIndex = leftChildIndex; | ||||||
} | ||||||
} | ||||||
|
||||||
if (rightChildIndex < this.store.length) { | ||||||
rightChild = this.store[rightChildIndex]; | ||||||
|
||||||
if (rightChild.key < this.store[smallestItemIndex].key) { | ||||||
smallestItemIndex = rightChildIndex; | ||||||
} | ||||||
} | ||||||
|
||||||
// base case | ||||||
if (smallestItemIndex === index) { | ||||||
return; | ||||||
} | ||||||
// swap | ||||||
// perform heapDown | ||||||
this.swap(index, smallestItemIndex); | ||||||
this.heapDown(smallestItemIndex); | ||||||
} | ||||||
|
||||||
// If you want a swap method... you're welcome | ||||||
|
@@ -68,5 +132,6 @@ class MinHeap { | |||||
} | ||||||
|
||||||
module.exports = { | ||||||
MinHeap | ||||||
MinHeap, | ||||||
HeapNode | ||||||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✨