fix heap sort algo
This commit is contained in:
parent
1ac446f9c8
commit
b36b6c759e
@ -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
|
||||
}
|
||||
|
||||
|
@ -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(""))
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user