# 标量中冒泡的随机整数排序

``````object BubbleSort {

def bubbleSort(array: Array[Int]) = {
def bubbleSortRecursive(array: Array[Int], current: Int, to: Int): Array[Int] = {
println(array.mkString(",") + "    current -> " + current + ", to -> " + to)
to match {
case 0 => array
case _ if(to == current) => bubbleSortRecursive(array, 0, to - 1)
case _ =>
if (array(current) > array(current + 1)) {
var temp = array(current + 1)
array(current + 1) = array(current)
array(current) = temp
}
bubbleSortRecursive(array, current + 1, to)
}
}
bubbleSortRecursive(array, 0, array.size - 1)
}

def main(args: Array[String]) {
val sortedArray = bubbleSort(Array(10,9,11,5,2))
println("Sorted Array -> " + sortedArray.mkString(","))
}
}
``````

Try this:

``````import scala.util.Random
val sortedArray = (1 to 10).map(_ => Random.nextInt).toArray
``````

You can use `scala.util.Random` for generation. `nextInt` method takes `maxValue` argument, so in the code sample, you'll generate list of 10 int values from 0 to 100.

``````val r = scala.util.Random
for (i <- 1 to 10) yield r.nextInt(100)
``````

You can find more info here or here