3.6 KiB
3.6 KiB
id, title, challengeType, forumTopicId
id | title | challengeType | forumTopicId |
---|---|---|---|
587d825b367417b2b2512c8c | Implement Heap Sort with a Min Heap | 1 | 301643 |
Description
Instructions
MinHeap
with insert
, remove
, and sort
methods. The sort
method should return an array of all the elements in the min heap sorted from smallest to largest.
Tests
tests:
- text: The MinHeap data structure should exist.
testString: assert((function() { var test = false; if (typeof MinHeap !== 'undefined') { test = new MinHeap() }; return (typeof test == 'object')})());
- text: MinHeap should have a method called insert.
testString: assert((function() { var test = false; if (typeof MinHeap !== 'undefined') { test = new MinHeap() } else { return false; }; return (typeof test.insert == 'function')})());
- text: MinHeap should have a method called remove.
testString: assert((function() { var test = false; if (typeof MinHeap !== 'undefined') { test = new MinHeap() } else { return false; }; return (typeof test.remove == 'function')})());
- text: MinHeap should have a method called sort.
testString: assert((function() { var test = false; if (typeof MinHeap !== 'undefined') { test = new MinHeap() } else { return false; }; return (typeof test.sort == 'function')})());
- text: The sort method should return an array containing all items added to the min heap in sorted order.
testString: |+
assert((() => {
if (typeof MinHeap === "undefined") {
return false;
}
const heap = new MinHeap();
const arr = createRandomArray(25);
for (let i of arr) {
heap.insert(i);
}
const result = heap.sort();
arr.sort((a, b) => a - b);
for (let i = 0; i < arr.length; i++) {
if (arr[i] !== result[i]) {
return false;
}
}
return true;
})());
Challenge Seed
function isSorted(a){
for(let i = 0; i < a.length - 1; i++)
if(a[i] > a[i + 1])
return false;
return true;
}
// Generate a randomly filled array
function createRandomArray(size = 5){
let a = new Array(size);
for(let i = 0; i < size; i++)
a[i] = Math.floor(Math.random() * 100);
return a;
}
const array = createRandomArray(25);
var MinHeap = function() {
// Only change code below this line
// Only change code above this line
};
Solution
// solution required