【发布时间】:2014-04-28 05:38:38
【问题描述】:
我以自己的方式实现了递归版本的选择和快速排序,我正在尝试修改代码,使其可以对任何泛型类型的列表进行排序,我想假设提供的泛型类型可以转换在运行时可比较。
有没有人有链接,代码或教程如何做到这一点? 我正在尝试修改此特定代码
'def main (args:Array[String]){
val l = List(2,4,5,6,8)
print(quickSort(l))
}
def quickSort(x:List[Int]):List[Int]={
x match{
case xh::xt =>
{
val (first,pivot,second) = partition(x)
quickSort (first):::(pivot :: quickSort(second))
}
case Nil => {x}
}
}
def partition (x:List[Int])=
{
val pivot =x.head
var first:List[Int]=List ()
var second : List[Int]=List ()
val fun=(i:Int)=> {
if (i<pivot)
first=i::first
else
second=i::second
}
x.tail.foreach(fun)
(first,pivot,second)
}
enter code here
def main (args:Array[String]){
val l = List(2,4,5,6,8)
print(quickSort(l))
}
def quickSort(x:List[Int]):List[Int]={
x match{
case xh::xt =>
{
val (first,pivot,second) = partition(x)
quickSort (first):::(pivot :: quickSort(second))
}
case Nil => {x}
}
}
def partition (x:List[Int])=
{
val pivot =x.head
var first:List[Int]=List ()
var second : List[Int]=List ()
val fun=(i:Int)=> {
if (i<pivot)
first=i::first
else
second=i::second
}
x.tail.foreach(fun)
(first,pivot,second)
} '
语言:斯卡拉
【问题讨论】:
-
“平台:SCALA 语言:JAVA”……所以……它是什么?
-
抱歉拼写错误,语言是scala,平台是eclipse
-
eclipse和它有什么关系?您的排序算法对执行它的 IDE 是明智的吗?怎么样?
标签: scala