Skip to content

Instantly share code, notes, and snippets.

@GuanshanLiu
Created September 29, 2018 12:25
Show Gist options
  • Save GuanshanLiu/30166eb1017e148cecbe69b59c11d1ea to your computer and use it in GitHub Desktop.
Save GuanshanLiu/30166eb1017e148cecbe69b59c11d1ea to your computer and use it in GitHub Desktop.
extension Heap {
static func sort(_ elements: [Element], sort: @escaping (Element, Element) -> Bool) -> [Element] {
var heap = Heap(sort: sort)
for e in elements {
heap.insert(e)
}
var result = [Element]()
while !heap.isEmpty {
result.append(heap.remove()!)
}
return result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment