页面树结构

2017-07-25 Apache Spark 2.2.0 官方文档中文版发布 : http://spark.apachecn.org/docs/cn/2.2.0/


MachineLearning 优酷地址 : http://i.youku.com/apachecn

转至元数据结尾
转至元数据起始

Scala
package DataStructureAndAlgorithm

/**
  * Created by jiangzl on 2016/10/28.
  */
object MergeSort {

  def mergedSort[T](less: (T, T) => Boolean)(list: List[T]): List[T] = {

    // 找到第一个最小的值,然后递归的进行比较(less只是一个def函数)
    def merged(xList: List[T], yList: List[T]): List[T] = {
      (xList, yList) match {
        case (Nil, _) => yList
        case (_, Nil) => xList
        case (x :: xTail, y :: yTail) => {
          if (less(x, y)) x :: merged(xTail, yList)
          else
            y :: merged(xList, yTail)
        }
      }
    }

    val n = list.length / 2
    if (n == 0) list
    else {
      // splitAt 从n个元素开始切分,把数据分为2份
      val (x, y) = list splitAt n
      // 合并左右的数组
      merged(mergedSort(less)(x), mergedSort(less)(y))
    }
  }

  def main(args: Array[String]) {
    val list = List(1, 2, 3, 7, 12, 20, 23, 43)
    println(mergedSort((x: Int, y: Int) => x < y)(list))
  }
}

结果
List(1, 2, 3, 7, 12, 20, 23, 43)