import scala.util.Random
/**
* Created by fhqplzj on 16-9-29 at 下午8:09.
*/
object MergeSortInScala {
var aux: Array[Int] = _
/**
* 归并算子
* 归并排序的核心部分
*
* @param nums
* @param lo
* @param mid
* @param hi
*/
def merge(nums: Array[Int], lo: Int, mid: Int, hi: Int): Unit = {
Array.copy(nums, lo, aux, lo, hi - lo + 1)
var (i, j) = (lo, mid + 1)
for (k <- lo to hi) {
if (i > mid) {
nums(k) = aux(j)
j += 1
} else if (j > hi) {
nums(k) = aux(i)
i += 1
} else if (aux(i) < aux(j)) {
nums(k) = aux(i)
i += 1
} else {
nums(k) = aux(j)
j += 1
}
}
}
/**
* 自顶而下的归并排序
*
* @param nums
* @param lo
* @param hi
*/
def sort(nums: Array[Int], lo: Int, hi: Int): Unit = {
if (lo < h
归并排序,scala实现
最新推荐文章于 2021-03-15 18:55:26 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)