Recent Changes - Search:

Oktatás

* Programozás 2
  + feladatsor
  + C feladatsor
  + Python feladatsor
  + GitHub oldal

* Szkriptnyelvek
  + feladatsor
  + quick link

* levelezősök
  + Adator. prog.
  + feladatsor
  + quick link

teaching assets


Félévek

* 2024/25/1
* archívum


Linkek

* kalendárium
   - munkaszüneti napok '20
* tételsorok
* jegyzetek
* szakdolgozat / PhD
* ösztöndíjak
* certificates
* C lang.
* C++
* C#
* Clojure
* D lang.
* Java
* Nim
* Scala


[ edit | logout ]
[ sandbox | passwd ]

sorting

#############################

Produce a sorted copy

scala> 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 place

scala> 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 ArrayBuffer

Quicksort 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.

Cloud City

  

Blogjaim, hobbi projektjeim

* The Ubuntu Incident
* Python Adventures
* @GitHub
* heroku
* extra
* haladó Python
* YouTube listák


Debrecen | la France


[ edit ]

Edit - History - Print *** Report - Recent Changes - Search
Page last modified on 2015 March 15, 10:25