diff --git a/src/main/kotlin/it/norangeb/algorithms/exercises/OrderedListSorter.kt b/src/main/kotlin/it/norangeb/algorithms/exercises/OrderedListSorter.kt index e50d0e1..aab7296 100644 --- a/src/main/kotlin/it/norangeb/algorithms/exercises/OrderedListSorter.kt +++ b/src/main/kotlin/it/norangeb/algorithms/exercises/OrderedListSorter.kt @@ -25,8 +25,10 @@ package it.norangeb.algorithms.exercises +import it.norangeb.algorithms.datastructures.queue.priority.BinaryHeap + object OrderedListSorter { - fun sort(vararg lists: List): List { + fun sortUnsignedInt(vararg lists: List): List { val bias = lists.map { it.first() }.sorted().first() val upperBound = lists.map { it.last() }.sorted().last() val bucket = Array(upperBound - bias + 1) { 0 } @@ -55,4 +57,28 @@ object OrderedListSorter { return (outList as Array).toList() } + + fun > sort(vararg lists: List): List { + val heap = BinaryHeap.createMinPriorityQueue, T> { it.value } + lists.forEachIndexed { index, list -> + heap.insert(Node(list.first(), index, 0)) + } + + val outList = ArrayList() + + 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(val value: T, val k: Int, val n: Int) } \ No newline at end of file diff --git a/src/test/kotlin/it/norangeb/algorithms/exercises/OrderedListSorterTest.kt b/src/test/kotlin/it/norangeb/algorithms/exercises/OrderedListSorterTest.kt index 4f0f815..0e5ab10 100644 --- a/src/test/kotlin/it/norangeb/algorithms/exercises/OrderedListSorterTest.kt +++ b/src/test/kotlin/it/norangeb/algorithms/exercises/OrderedListSorterTest.kt @@ -26,22 +26,52 @@ package it.norangeb.algorithms.exercises import org.amshove.kluent.`should equal` +import org.junit.Before import org.junit.Test class OrderedListSorterTest { - @Test - fun test() { - val out = OrderedListSorter.sort( + lateinit var lists: List> + + @Before + fun init() { + lists = listOf( 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(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) ) + } + + @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, 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 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") + } } \ No newline at end of file