Oktatás * Programozás 2 * Szkriptnyelvek * levelezősök Félévek Linkek * kalendárium |
Scala /
sorting
Produce a sorted copyscala> val a = Array(20, 5, 4) a: Array[Int] = Array(20, 5, 4) scala> a.sorted // gives back a sorted copy res0: Array[Int] = Array(4, 5, 20) scala> val b = scala.collection.mutable.ArrayBuffer(20, 5, 4) b: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(20, 5, 4) scala> b.sorted // gives back a sorted copy res1: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(4, 5, 20) scala> a res2: Array[Int] = Array(20, 5, 4) // the original is not changed scala> b res3: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(20, 5, 4) // the original is not changed Sort an Array in placescala> a res4: Array[Int] = Array(20, 5, 4) scala> scala.util.Sorting.quickSort(a) // sorts the array in place scala> a res6: Array[Int] = Array(4, 5, 20) Sort an ArrayBufferQuicksort doesn't work with array buffers. You can do something like this: scala> val b = scala.collection.mutable.ArrayBuffer(20, 5, 4) b: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(20, 5, 4) scala> scala.util.Sorting.stableSort(b) res16: Array[Int] = Array(4, 5, 20) // it gives back a sorted Array scala> b res17: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(20, 5, 4) // but "b" hasn't changed Or, convert the array buffer to array and use quicksort on this array. |
Blogjaim, hobbi projektjeim * The Ubuntu Incident [ edit ] |