基于scala实现冒泡排序算法,将6,-2,5,9,3,8,3,7,7,2,12,689349,0,130189723,132908,4,9800,3,2,4,54,854,0,11,-99,-454
时间: 2023-10-08 15:10:53 浏览: 98
用冒泡排序这一算法排序数组
基于Scala的冒泡排序算法的实现如下:
```scala
object BubbleSort {
def main(args: Array[String]): Unit = {
val nums = Array(6, -2, 5, 9, 3, 8, 3, 7, 7, 2, 12, 689349, 0, 130189723, 132908, 4, 9800, 3, 2, 4, 54, 854, 0, 11, -99, -454)
bubbleSort(nums)
println(nums.mkString(", "))
}
def bubbleSort(nums: Array[Int]): Unit = {
for (i <- 0 until nums.length - 1) {
for (j <- 0 until nums.length - i - 1) {
if (nums(j) > nums(j + 1)) {
val temp = nums(j)
nums(j) = nums(j + 1)
nums(j + 1) = temp
}
}
}
}
}
```
结果为:-454, -99, -2, 0, 0, 2, 2, 3, 3, 3, 4, 4, 5, 6, 7, 7, 8, 9, 11, 12, 54, 854, 9800, 132908, 689349, 130189723
阅读全文