fix heap sort algo

This commit is contained in:
Ivan Li 2019-01-12 20:45:41 +08:00
parent 1ac446f9c8
commit b36b6c759e
2 changed files with 8 additions and 8 deletions

View File

@ -47,9 +47,9 @@ class Heap(val capacity: Int, var elementCount: Int = 0) {
def removeMax(): Int = {
require(elementCount > 0, "heap is empty")
val result = array(1)
elementCount -= 1
array(1) = array(elementCount)
heapifyTopDown(1, elementCount - 1)
elementCount -= 1
heapifyTopDown(1, elementCount)
result
}

View File

@ -31,11 +31,11 @@ class HeapTest extends FlatSpec with Matchers {
heap.removeMax() should equal(27)
}
// it should "sort array" in {
// val nums = Array(33, 27, 21, 16, 13, 15, 19, 5, 6, 7, 8, 1, 2, 12)
// val result = Heap.heapSort(nums)
//
// result.mkString("") should equal(nums.sorted.mkString(""))
// }
it should "sort array" in {
val nums = Array(33, 27, 21, 16, 13, 15, 19, 5, 6, 7, 8, 1, 2, 12)
val result = Heap.heapSort(nums)
result.mkString("") should equal(nums.sorted.mkString(""))
}
}