mirror of
https://github.com/jlengrand/kotlin.git
synced 2026-05-09 00:21:47 +00:00
42 lines
776 B
Plaintext
42 lines
776 B
Plaintext
fun IntArray.swap(i:Int, j:Int) {
|
|
val temp = this[i]
|
|
this[i] = this[j]
|
|
this[j] = temp
|
|
}
|
|
|
|
fun IntArray.quicksort() = quicksort(0, size-1)
|
|
|
|
fun IntArray.quicksort(L: Int, R:Int) {
|
|
val m = this[(L + R) / 2]
|
|
var i = L
|
|
var j = R
|
|
while (i <= j) {
|
|
while (this[i] < m)
|
|
i++
|
|
while (this[j] > m)
|
|
j--
|
|
if (i <= j) {
|
|
swap(i++, j--)
|
|
}
|
|
else {
|
|
}
|
|
}
|
|
if (L < j)
|
|
quicksort(L, j)
|
|
if (R > i)
|
|
quicksort(i, R)
|
|
}
|
|
|
|
fun box() : String {
|
|
val a = IntArray(10)
|
|
for(i in 0..4) {
|
|
a[2*i] = 2*i
|
|
a[2*i+1] = -2*i-1
|
|
}
|
|
a.quicksort()
|
|
for(i in 0..9) {
|
|
System.out?.print(a[i])
|
|
System.out?.print(' ')
|
|
}
|
|
return "OK"
|
|
} |