dev merge sort
This commit is contained in:
parent
569cc1c0d2
commit
5b3ad97fcb
60
scala/src/main/scala/ch12_sorts/MergeSort.scala
Normal file
60
scala/src/main/scala/ch12_sorts/MergeSort.scala
Normal file
@ -0,0 +1,60 @@
|
||||
package ch12_sorts
|
||||
|
||||
object MergeSort {
|
||||
|
||||
def mergeSort(items: Array[Int]): Array[Int] = {
|
||||
_mergeSort(items, 0, items.length - 1)
|
||||
items
|
||||
}
|
||||
|
||||
|
||||
private[this] def _mergeSort(items: Array[Int], p: Int, r: Int): Unit = {
|
||||
if (p >= r) {
|
||||
return
|
||||
}
|
||||
|
||||
val q = p + (r - p) / 2
|
||||
_mergeSort(items, p, q)
|
||||
_mergeSort(items, q + 1, r)
|
||||
_merge(items, p, q, r)
|
||||
|
||||
}
|
||||
|
||||
private[this] def _merge(items: Array[Int], p: Int, q: Int, r: Int): Unit = {
|
||||
//start of first half
|
||||
var i = p
|
||||
//start of second half
|
||||
var j = q + 1
|
||||
var k = 0
|
||||
//temp array to hold the data
|
||||
val tempArray = new Array[Int](r - p + 1)
|
||||
while (i <= q && j <= r) {
|
||||
if (items(i) <= items(j)) {
|
||||
tempArray(k) = items(i)
|
||||
i += 1
|
||||
} else {
|
||||
tempArray(k) = items(j)
|
||||
j += 1
|
||||
}
|
||||
k += 1
|
||||
}
|
||||
|
||||
var start = i
|
||||
var end = q
|
||||
|
||||
if (j <= r) {
|
||||
start = j
|
||||
end = r
|
||||
}
|
||||
|
||||
for (n <- start to end) {
|
||||
tempArray(k) = items(n)
|
||||
k += 1
|
||||
}
|
||||
|
||||
//copy tempArray back to items
|
||||
for (n <- 0 to r - p) {
|
||||
items(p + n) = tempArray(n)
|
||||
}
|
||||
}
|
||||
}
|
@ -6,7 +6,7 @@ import scala.util.Random
|
||||
|
||||
class SortsTest extends FlatSpec with Matchers {
|
||||
|
||||
behavior of "SortsTest"
|
||||
behavior of "SortsTest in ch11"
|
||||
|
||||
it should "bubbleSort int arrays" in {
|
||||
var array = Array(4, 5, 6, 3, 2, 1)
|
||||
|
22
scala/src/test/scala/ch12_sorts/MergeSortTest.scala
Normal file
22
scala/src/test/scala/ch12_sorts/MergeSortTest.scala
Normal file
@ -0,0 +1,22 @@
|
||||
package ch12_sorts
|
||||
|
||||
|
||||
import org.scalatest.{FlatSpec, Matchers}
|
||||
|
||||
class MergeSortTest extends FlatSpec with Matchers {
|
||||
behavior of "SortsTest in ch12"
|
||||
|
||||
it should "mergeSort int arrays" in {
|
||||
var array = Array(4, 5, 6, 3, 2, 1)
|
||||
array = MergeSort.mergeSort(array)
|
||||
array.mkString("") should equal("123456")
|
||||
|
||||
array = Array(4)
|
||||
array = MergeSort.mergeSort(array)
|
||||
array.mkString("") should equal("4")
|
||||
|
||||
array = Array(4, 2)
|
||||
array = MergeSort.mergeSort(array)
|
||||
array.mkString("") should equal("24")
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user