add OrderedListSorter
All checks were successful
continuous-integration/drone/push Build is passing

now it work only with non negative integer list
This commit is contained in:
Raffaele Mignone 2019-04-12 16:26:55 +02:00
parent a45eac427e
commit 471b0413cc
Signed by: norangebit
GPG Key ID: F5255658CB220573
2 changed files with 105 additions and 0 deletions

View File

@ -0,0 +1,58 @@
/*
* MIT License
*
* Copyright (c) 2019 norangebit
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
package it.norangeb.algorithms.exercises
object OrderedListSorter {
fun sort(vararg lists: List<Int>): List<Int> {
val bias = lists.map { it.first() }.sorted().first()
val upperBound = lists.map { it.last() }.sorted().last()
val bucket = Array(upperBound - bias + 1) { 0 }
lists.forEach {
it.forEach {
bucket[it - bias] += 1
}
}
bucket.forEachIndexed { index, _ ->
if (index == 0)
return@forEachIndexed
bucket[index] += bucket[index - 1]
}
val outList = arrayOfNulls<Int>(bucket[upperBound - bias])
lists.forEach {
it.forEach {
outList[bucket[it - bias] - 1] = it
bucket[it - bias] -= 1
}
}
return (outList as Array<Int>).toList()
}
}

View File

@ -0,0 +1,47 @@
/*
* MIT License
*
* Copyright (c) 2019 norangebit
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
package it.norangeb.algorithms.exercises
import org.amshove.kluent.`should equal`
import org.junit.Test
class OrderedListSorterTest {
@Test
fun test() {
val out = OrderedListSorter.sort(
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)
)
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)
}
}