package main
import "fmt"
func main(){
numArr := [...]int{4,2,9,45,8,36,5,1,82}
numCount := len(numArr)
for i:=0;i<numCount;i++{
for j:=numCount-1;j>0;j--{
if numArr[j] < numArr[j-1] {
numArr[j],numArr[j-1] = numArr[j-1],numArr[j]
}
}
}
fmt.Println(numArr)
}
还是Scala的语法简洁,奈何我大Go语言编译部署快啊
```
/**
* 快速排序
*/
object QuickSort {
//由于不是尾递归,会造成栈溢出
def sort[T](list: List[T])(implicit ord: Ordering[T]): List[T] = {
//模式匹配用法
list match {
case Nil => Nil
case head :: tail => {
val (left, right) = tail.partition { ord.compare(_, head) < 0 }
sort(left) ::: head :: sort(right)
}
}
}
def main(args: Array[String]): Unit = {
val list = List.range(1, 10)
println(sort(list))
println(sort(list)(Ordering.Int.reverse))
}
}
```
#2
更多评论
/**
* 快速排序
*/
object QuickSort {
//由于不是尾递归,会造成栈溢出
def sort[T](list: List[T])(implicit ord: Ordering[T]): List[T] = {
//模式匹配用法
list match {
case Nil => Nil
case head :: tail => {
val (left, right) = tail.partition { ord.compare(_, head) < 0 }
sort(left) ::: head :: sort(right)
}
}
}
def main(args: Array[String]): Unit = {
val list = List.range(1, 10)
println(sort(list))
println(sort(list)(Ordering.Int.reverse))
}
}
#1