Update all new Tasks
This commit is contained in:
parent
00a190b0a6
commit
91df62d461
5697 changed files with 93386 additions and 804 deletions
20
Task/Quickselect-algorithm/Scala/quickselect-algorithm.scala
Normal file
20
Task/Quickselect-algorithm/Scala/quickselect-algorithm.scala
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
import scala.util.Random
|
||||
|
||||
object QuickSelect {
|
||||
def quickSelect[A <% Ordered[A]](seq: Seq[A], n: Int, rand: Random = new Random): A = {
|
||||
val pivot = rand.nextInt(seq.length);
|
||||
val (left, right) = seq.partition(_ < seq(pivot))
|
||||
if (left.length == n) {
|
||||
seq(pivot)
|
||||
} else if (left.length < n) {
|
||||
quickSelect(right, n - left.length, rand)
|
||||
} else {
|
||||
quickSelect(left, n, rand)
|
||||
}
|
||||
}
|
||||
|
||||
def main(args: Array[String]): Unit = {
|
||||
val v = Array(9, 8, 7, 6, 5, 0, 1, 2, 3, 4)
|
||||
println((0 until v.length).map(quickSelect(v, _)).mkString(", "))
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue