September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -1,13 +1,12 @@
import java.util.*
import java.util.Comparator
import java.util.ArrayList
fun <T> quickSort(a : List<T>, c : Comparator<T>) : ArrayList<T> {
return if (a.size == 0) ArrayList(a)
else {
val boxes = Array<ArrayList<T>>(3, {ArrayList<T>()})
fun normalise(i : Int) = i / Math.max(1, Math.abs(i))
a forEach {boxes[normalise(c.compare(it, a[0])) + 1] add(it)}
array(0, 2) forEach {boxes[it] = quickSort(boxes[it], c)}
boxes.flatMapTo(ArrayList<T>()) {it}
}
fun <T> quickSort(a: List<T>, c: Comparator<T>): ArrayList<T> {
if (a.isEmpty()) return ArrayList(a)
val boxes = Array(3, { ArrayList<T>() })
fun normalise(i: Int) = i / Math.max(1, Math.abs(i))
a.forEach { boxes[normalise(c.compare(it, a[0])) + 1].add(it) }
arrayOf(0, 2).forEach { boxes[it] = quickSort(boxes[it], c) }
return boxes.flatMapTo(ArrayList<T>()) { it }
}

View file

@ -1,15 +1,13 @@
fun quicksort(list: List<Int>): List<Int> {
if (list.size == 0) {
return listOf()
} else {
val head = list.first()
val tail = list.takeLast(list.size - 1)
if (list.isEmpty()) return emptyList()
val less = quicksort(tail.filter { it < head })
val high = quicksort(tail.filter { it >= head })
val head = list.first()
val tail = list.takeLast(list.size - 1)
return less + head + high
}
val less = quicksort(tail.filter { it < head })
val high = quicksort(tail.filter { it >= head })
return less + head + high
}
fun main(args: Array<String>) {