Skip to content

Instantly share code, notes, and snippets.

@lambda-fairy
Created December 6, 2012 02:51
Show Gist options
  • Save lambda-fairy/4221445 to your computer and use it in GitHub Desktop.
Save lambda-fairy/4221445 to your computer and use it in GitHub Desktop.
Inefficient priority queue
/**
* Extremely inefficient priority queue.
*
* In a real program this would probably be implemented as a heap,
* but this is good enough for this demonstration.
*/
function PQueue(comparator) {
this.comparator = comparator || function(x, y) { return x - y }
this.array = []
}
PQueue.prototype.add = function(item) {
this.array.push(item)
this.heapify()
}
PQueue.prototype.addAll = function(items) {
for (var i = 0; i < items.length; ++i) {
this.array.push(items[i])
}
this.heapify()
}
PQueue.prototype.heapify = function() {
// lol
this.array.sort(this.comparator)
}
PQueue.prototype.size = function() {
return this.array.length
}
PQueue.prototype.shift = function() {
return this.array.shift()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment