Skip to content

Commit ad0d122

Browse files
committed
impl heap sort
1 parent 009ef35 commit ad0d122

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

scala/src/main/scala/ch28_heap/Heap.scala

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@ import scala.util.control.Breaks._
44

55
class Heap(val capacity: Int, var elementCount: Int = 0) {
66

7+
def this(arrayParam: Array[Int], bottomUp: Boolean) = {
8+
this(arrayParam.length + 1)
9+
if (bottomUp) {
10+
arrayParam.foreach(this.insert)
11+
} else {
12+
//copy data into array of heap
13+
for (i <- arrayParam.indices) {
14+
array(i + 1) = arrayParam(i)
15+
elementCount = arrayParam.length
16+
}
17+
for (i <- elementCount / 2 + 1 to 1 by -1) {
18+
heapifyTopDown(i, elementCount - 1)
19+
}
20+
}
21+
}
22+
723
require(capacity > 0, "capacity should be > 0")
824
val array: Array[Int] = new Array[Int](capacity)
925

@@ -41,7 +57,7 @@ class Heap(val capacity: Int, var elementCount: Int = 0) {
4157
//start from the top to compare with it's child nodes
4258
//swap if child node > parent node
4359
//stop at child node <= parent node
44-
private[this] def heapifyTopDown(startIndex: Int, stopIndex: Int) = {
60+
private[this] def heapifyTopDown(startIndex: Int, stopIndex: Int) = {
4561
var pointer = startIndex
4662
breakable {
4763
while (true) {
@@ -66,3 +82,14 @@ class Heap(val capacity: Int, var elementCount: Int = 0) {
6682
}
6783

6884
}
85+
86+
object Heap {
87+
def heapSort(array: Array[Int]): Array[Int] = {
88+
val result = new Array[Int](array.length)
89+
val heap = new Heap(array, true)
90+
for (i <- result.length - 1 to 0 by -1) {
91+
result(i) = heap.removeMax()
92+
}
93+
result
94+
}
95+
}

scala/src/test/scala/ch11_sorts/SortsTest.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ch11_sorts
22

33
import ch12_sorts.{MergeSort, QuickSort}
4+
import ch28_heap.Heap
45
import org.scalatest.{FlatSpec, Matchers}
56

67
import scala.util.Random
@@ -53,6 +54,7 @@ class SortsTest extends FlatSpec with Matchers {
5354
timed("selectionSort", Sorts.selectionSort, array.clone())
5455
timed("mergeSort", MergeSort.mergeSort, array.clone())
5556
timed("quickSort", QuickSort.quickSort, array.clone())
57+
timed("heapSort", Heap.heapSort, array.clone())
5658
}
5759

5860
def reportElapsed(name: String, time: Long): Unit = println(name + " takes in " + time + "ms")

scala/src/test/scala/ch28_heap/HeapTest.scala

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,29 @@ class HeapTest extends FlatSpec with Matchers {
1515
heap.removeMax() should equal(27)
1616
}
1717

18+
it should "build heap from array - bottom up" in {
19+
val nums = Array(33, 27, 21, 16, 13, 15, 19, 5, 6, 7, 8, 1, 2, 12)
20+
val heap = new Heap(nums, true)
21+
22+
heap.removeMax() should equal(33)
23+
heap.removeMax() should equal(27)
24+
}
25+
26+
it should "build heap from array - top down" in {
27+
val nums = Array(33, 27, 21, 16, 13, 15, 19, 5, 6, 7, 8, 1, 2, 12)
28+
val heap = new Heap(nums, false)
29+
30+
heap.removeMax() should equal(33)
31+
heap.removeMax() should equal(27)
32+
}
33+
34+
it should "sort array" in {
35+
it should "build heap from array - top down" in {
36+
val nums = Array(33, 27, 21, 16, 13, 15, 19, 5, 6, 7, 8, 1, 2, 12)
37+
val result = Heap.heapSort(nums)
38+
39+
result.mkString("") should equal(nums.sorted.mkString(""))
40+
}
41+
}
42+
1843
}

0 commit comments

Comments
 (0)