页面树结构

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 QuickSort {
  def quickSort(list: List[Int]): List[Int] = list match {
      case Nil => Nil
      case List() => List()
      case head :: tail =>
        // 数组,按照首元素,分成左右2个partition
        val (left, right) = tail.partition(_ < head)
        quickSort(left) ::: head :: quickSort(right)
    }

  def main(args: Array[String]) {
    val list = List(3, 12, 43, 23, 7, 1, 2, 20)
    println(quickSort(list))
  }
}

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

评论

  1. Sehriff 发表:

    scala> val(l,r)=List(3, 12, 43, 23, 7, 20).partition(_<3)
    l: List[Int] = List()
    r: List[Int] = List(3, 12, 43, 23, 7, 20)

    scala> val(ll,rr)=List(3, 12, 43, 23, 7, 20).partition(_<=3)
    ll: List[Int] = List(3)
    rr: List[Int] = List(12, 43, 23, 7, 20)