scala排序demo

背景

今天和dior大师在胡扯面试算法,说白板写个排序,然后两人互彪了一发演技:

case 1


def sortMatch(xs:Array[Int]):Array[Int] = xs.length match {
  case s if s < 1 => xs
  case _ => val mid = xs(xs.length/2)
    Array.concat(sortMatch(xs.filter(_ < mid)),xs.filter(_ == mid),sortMatch(xs.filter( _ > mid)))
}

代码也很简单,然而dior表示不服:

case 2


def quickSort(xs: List[Int]): List[Int] = xs match {
  case Nil => Nil
  case head :: tail =>
    val (before, after) = tail.partition(_ < head)
    quickSort(before) ++ (head :: quickSort(after))
}

发现其实实现真的很多,哈哈,小记一下
我的github

你可能感兴趣的:(scala)