Skip to content

Commit

Permalink
fix heap sort algo
Browse files Browse the repository at this point in the history
  • Loading branch information
email2liyang committed Jan 12, 2019
1 parent 1ac446f commit b36b6c7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions scala/src/main/scala/ch28_heap/Heap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
12 changes: 6 additions & 6 deletions scala/src/test/scala/ch28_heap/HeapTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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(""))
}

}

0 comments on commit b36b6c7

Please sign in to comment.