update OrderedListSorter
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
now it work with all comparable type
This commit is contained in:
parent
471b0413cc
commit
a9bd9df609
@ -25,8 +25,10 @@
|
|||||||
|
|
||||||
package it.norangeb.algorithms.exercises
|
package it.norangeb.algorithms.exercises
|
||||||
|
|
||||||
|
import it.norangeb.algorithms.datastructures.queue.priority.BinaryHeap
|
||||||
|
|
||||||
object OrderedListSorter {
|
object OrderedListSorter {
|
||||||
fun sort(vararg lists: List<Int>): List<Int> {
|
fun sortUnsignedInt(vararg lists: List<Int>): List<Int> {
|
||||||
val bias = lists.map { it.first() }.sorted().first()
|
val bias = lists.map { it.first() }.sorted().first()
|
||||||
val upperBound = lists.map { it.last() }.sorted().last()
|
val upperBound = lists.map { it.last() }.sorted().last()
|
||||||
val bucket = Array(upperBound - bias + 1) { 0 }
|
val bucket = Array(upperBound - bias + 1) { 0 }
|
||||||
@ -55,4 +57,28 @@ object OrderedListSorter {
|
|||||||
|
|
||||||
return (outList as Array<Int>).toList()
|
return (outList as Array<Int>).toList()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun <T : Comparable<T>> sort(vararg lists: List<T>): List<T> {
|
||||||
|
val heap = BinaryHeap.createMinPriorityQueue<Node<T>, T> { it.value }
|
||||||
|
lists.forEachIndexed { index, list ->
|
||||||
|
heap.insert(Node(list.first(), index, 0))
|
||||||
|
}
|
||||||
|
|
||||||
|
val outList = ArrayList<T>()
|
||||||
|
|
||||||
|
while (!heap.isEmpty()) {
|
||||||
|
heap.pop().map {
|
||||||
|
outList.add(it.value)
|
||||||
|
val k = it.k
|
||||||
|
val n = it.n + 1
|
||||||
|
|
||||||
|
if (lists[k].size > n)
|
||||||
|
heap.insert(Node(lists[k][n], k, n))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return outList
|
||||||
|
}
|
||||||
|
|
||||||
|
data class Node<T>(val value: T, val k: Int, val n: Int)
|
||||||
}
|
}
|
@ -26,22 +26,52 @@
|
|||||||
package it.norangeb.algorithms.exercises
|
package it.norangeb.algorithms.exercises
|
||||||
|
|
||||||
import org.amshove.kluent.`should equal`
|
import org.amshove.kluent.`should equal`
|
||||||
|
import org.junit.Before
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
|
|
||||||
class OrderedListSorterTest {
|
class OrderedListSorterTest {
|
||||||
|
|
||||||
@Test
|
lateinit var lists: List<List<Int>>
|
||||||
fun test() {
|
|
||||||
val out = OrderedListSorter.sort(
|
@Before
|
||||||
|
fun init() {
|
||||||
|
lists = listOf(
|
||||||
listOf(1, 5, 9, 10, 12, 14, 15, 15, 20, 25, 29, 31, 35, 37, 43, 48),
|
listOf(1, 5, 9, 10, 12, 14, 15, 15, 20, 25, 29, 31, 35, 37, 43, 48),
|
||||||
listOf(2, 7, 12, 21, 23, 33, 35, 43, 45),
|
listOf(2, 7, 12, 21, 23, 33, 35, 43, 45),
|
||||||
listOf(3, 7, 9, 12, 14, 16, 27, 28, 37, 39, 45, 45, 46, 50),
|
listOf(3, 7, 9, 12, 14, 16, 27, 28, 37, 39, 45, 45, 46, 50),
|
||||||
listOf(1, 8, 9, 11, 13, 21, 25, 33, 34, 41, 47, 48)
|
listOf(1, 8, 9, 11, 13, 21, 25, 33, 34, 41, 47, 48)
|
||||||
)
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testSortUnsignedInt() {
|
||||||
|
val out = OrderedListSorter.sortUnsignedInt(*lists.toTypedArray())
|
||||||
|
|
||||||
out `should equal` listOf(1, 1, 2, 3, 5, 7, 7, 8, 9, 9, 9, 10, 11, 12,
|
out `should equal` listOf(1, 1, 2, 3, 5, 7, 7, 8, 9, 9, 9, 10, 11, 12,
|
||||||
12, 12, 13, 14, 14, 15, 15, 16, 20, 21, 21, 23, 25, 25, 27, 28, 29,
|
12, 12, 13, 14, 14, 15, 15, 16, 20, 21, 21, 23, 25, 25, 27, 28, 29,
|
||||||
31, 33, 33, 34, 35, 35, 37, 37, 39, 41, 43, 43, 45, 45, 45, 46, 47,
|
31, 33, 33, 34, 35, 35, 37, 37, 39, 41, 43, 43, 45, 45, 45, 46, 47,
|
||||||
48, 48, 50)
|
48, 48, 50)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testSortInt() {
|
||||||
|
val out = OrderedListSorter.sort(*lists.toTypedArray())
|
||||||
|
|
||||||
|
out `should equal` listOf(1, 1, 2, 3, 5, 7, 7, 8, 9, 9, 9, 10, 11, 12,
|
||||||
|
12, 12, 13, 14, 14, 15, 15, 16, 20, 21, 21, 23, 25, 25, 27, 28, 29,
|
||||||
|
31, 33, 33, 34, 35, 35, 37, 37, 39, 41, 43, 43, 45, 45, 45, 46, 47,
|
||||||
|
48, 48, 50)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testSortStrings() {
|
||||||
|
val out = OrderedListSorter.sort(
|
||||||
|
listOf("AAA", "BCE", "BCZ", "DCC", "DDD", "EEE"),
|
||||||
|
listOf("AAB", "AAC", "BBB", "DZZ", "EAA", "EAB"),
|
||||||
|
listOf("CCC", "CCZ", "DAA", "DDD")
|
||||||
|
)
|
||||||
|
|
||||||
|
out `should equal` listOf("AAA", "AAB", "AAC", "BBB", "BCE", "BCZ", "CCC", "CCZ", "DAA",
|
||||||
|
"DCC", "DDD", "DDD", "DZZ", "EAA", "EAB", "EEE")
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user