-
Notifications
You must be signed in to change notification settings - Fork 3
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
Dani Sanchez - Paper #1
base: master
Are you sure you want to change the base?
Changes from all commits
bd5b5e4
4f4a84d
ea87984
50002b9
78abf6b
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...'); | ||
const heap = new MinHeap(); | ||
|
||
while (list.length > 0) { | ||
heap.add(list.pop()); | ||
} | ||
|
||
while (!heap.isEmpty()) { | ||
list.push(heap.remove()); | ||
} | ||
|
||
return list; | ||
}; | ||
|
||
module.exports = heapsort; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,18 +11,31 @@ class MinHeap { | |
} | ||
|
||
// This method adds a HeapNode instance to the heap | ||
// Time Complexity: ? | ||
// Space Complexity: ? | ||
// Time Complexity: O(log n) | ||
// Space Complexity: O(log n) | ||
add(key, value = key) { | ||
throw new Error("Method not implemented yet..."); | ||
Comment on lines
+14
to
-17
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. 👍 |
||
|
||
const node = new HeapNode(key, value); | ||
this.store.push(node); | ||
if (this.store.length > 1) { | ||
this.heapUp(this.store.length - 1); | ||
} | ||
} | ||
|
||
// 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(log n) | ||
remove() { | ||
Comment on lines
+27
to
29
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; | ||
} | ||
|
||
this.swap(0, (this.store.length - 1)) | ||
const lastNode = this.store.pop(); | ||
this.heapDown(0); | ||
|
||
return lastNode.value | ||
} | ||
|
||
|
||
|
@@ -38,35 +51,61 @@ class MinHeap { | |
} | ||
|
||
// This method returns true if the heap is empty | ||
// Time complexity: ? | ||
// Space complexity: ? | ||
// Time complexity: O(1) | ||
// Space complexity: O(1) | ||
isEmpty() { | ||
Comment on lines
+54
to
56
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..."); | ||
return this.store.length === 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) | ||
// Space complexity: O(log n) | ||
heapUp(index) { | ||
Comment on lines
+63
to
65
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..."); | ||
|
||
const parentIndex = Math.floor((index - 1) / 2); | ||
const parentNode = this.store[parentIndex]; | ||
const childNode = this.store[index]; | ||
|
||
if (childNode.key < parentNode.key) { | ||
this.swap(index, parentIndex); | ||
if (parentIndex > 0) { | ||
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
80
to
83
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. 👍 Very compact |
||
throw new Error("Method not implemented yet..."); | ||
const leftNode = 2 * index + 1 | ||
const rightNode = 2 * index + 2 | ||
let smallest = index; | ||
|
||
if(leftNode < this.store.length && this.store[leftNode].key < this.store[smallest].key) { | ||
smallest = leftNode; | ||
} | ||
|
||
if (rightNode < this.store.length && this.store[rightNode].key < this.store[smallest].key) { | ||
smallest = rightNode; | ||
} | ||
|
||
if (index !== smallest){ | ||
this.swap(index, smallest); | ||
this.heapDown(smallest); | ||
} | ||
} | ||
|
||
// If you want a swap method... you're welcome | ||
swap(index1, index2) { | ||
const s = this.store; | ||
[s[index1], s[index2]] = [s[index2], s[index1]]; | ||
} | ||
} | ||
|
||
} | ||
module.exports = { | ||
MinHeap | ||
}; |
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.
👍