add sorting algorithms

- edit mergesort
- add quicksort
- add countingsort
This commit is contained in:
Raffaele Mignone 2019-06-09 12:40:01 +02:00
parent 0bac46d701
commit cac5af049f
Signed by: norangebit
GPG Key ID: F5255658CB220573
5 changed files with 247 additions and 14 deletions

View File

@ -0,0 +1,51 @@
/*
* 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.sorting
object Countingsort {
fun sort(array: Array<Int>, from: Int, to: Int) {
if (from >= to)
return
val range = to - from + 1
val bucket = Array(range) { 0 }
array.forEach { bucket[it - from] += 1 }
bucket.forEachIndexed { index, i ->
if (index != 0) {
bucket[index] += bucket[index - 1]
}
}
val aux = array.clone()
aux.forEach {
array[--bucket[it - from]] = it
}
}
}

View File

@ -27,13 +27,13 @@ package it.norangeb.algorithms.sorting
object Mergesort : Sorter() { object Mergesort : Sorter() {
override fun <T> sort(array: Array<T>, isLess: (T, T) -> Boolean) { override fun <T> sort(array: Array<T>, isLess: (T, T) -> Boolean) {
val auxiliary = arrayOfNulls<Any>(array.size) as Array<T> val aux = array.clone()
sort(array, auxiliary, 0, array.size - 1, isLess) sort(array, aux, 0, array.size - 1, isLess)
} }
private fun <T> sort( private fun <T> sort(
array: Array<T>, array: Array<T>,
auxiliary: Array<T>, aux: Array<T>,
low: Int, low: Int,
high: Int, high: Int,
isLess: (T, T) -> Boolean isLess: (T, T) -> Boolean
@ -42,35 +42,36 @@ object Mergesort : Sorter() {
val mid = low + (high - low) / 2 val mid = low + (high - low) / 2
sort(array, auxiliary, low, mid, isLess) sort(array, aux, low, mid, isLess)
sort(array, auxiliary, mid + 1, high, isLess) sort(array, aux, mid + 1, high, isLess)
if (!isLess(array[mid+1], array[mid])) if (isLess(array[mid], array[mid + 1]))
return return
merge(array, auxiliary, low, mid, high, isLess) merge(array, aux, low, mid, high, isLess)
} }
private inline fun <T> merge( private fun <T> merge(
array: Array<T>, array: Array<T>,
auxiliary: Array<T>, aux: Array<T>,
low: Int, low: Int,
mid: Int, mid: Int,
high: Int, high: Int,
isLess: (T, T) -> Boolean isLess: (T, T) -> Boolean
) { ) {
for (i in low..high) for (i in low..high)
auxiliary[i] = array[i] aux[i] = array[i]
var i = low var i = low
var j = mid + 1 var j = mid + 1
for (k in low..high) for (k in low..high)
when { when {
i > mid -> array[k] = auxiliary[j++] i > mid -> array[k] = aux[j++]
j > high -> array[k] = auxiliary[i++] j > high -> array[k] = aux[i++]
isLess(auxiliary[j], auxiliary[i]) -> array[k] = auxiliary[j++] isLess(aux[i], aux[j]) -> array[k] = aux[i++]
else -> array[k] = auxiliary[i++] else -> array[k] = aux[j++]
} }
} }
} }

View File

@ -0,0 +1,74 @@
/*
* 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.sorting
object Quicksort : Sorter() {
override fun <T> sort(array: Array<T>, isLess: (T, T) -> Boolean) {
sort(array, 0, array.size - 1, isLess)
}
private fun <T> sort(array: Array<T>, low: Int, high: Int, isLess: (T, T) -> Boolean) {
if (low >= high)
return
val mid = partitioning(array, low, high, isLess)
sort(array, low, mid - 1, isLess)
sort(array, mid + 1, high, isLess)
}
private fun <T> partitioning(
array: Array<T>,
low: Int,
high: Int,
isLess: (T, T) -> Boolean
): Int {
var i = low
var j = high + 1
while (true) {
while (isLess(array[++i], array[low]))
if (i == high) break
while (isLess(array[low], array[--j]))
if (j == low) break
if (i >= j) break
swap(array, i, j)
}
swap(array, low, j)
return j
}
private fun <T> swap(array: Array<T>, i: Int, j: Int) {
val tmp = array[i]
array[i] = array[j]
array[j] = tmp
}
}

View File

@ -0,0 +1,43 @@
/*
* 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.sorting
import org.amshove.kluent.`should equal`
import org.junit.jupiter.api.Test
class CountingsortTest {
@Test
fun testSort() {
val array = arrayOf(13, 15, 11, 4, 5, 3, 2, 7, 1, 2, 3, 3, 6, 9, 11)
val arrayCopy = array.clone()
Countingsort.sort(array, 1, 15)
array `should equal` arrayCopy.sortedArray()
}
}

View File

@ -0,0 +1,64 @@
/*
* 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.sorting
import org.amshove.kluent.`should equal`
import org.junit.jupiter.api.Test
class QuicksortTest {
@Test
fun testSort() {
val array = arrayOf(5, 2, 3, 1, 4)
val arrayCopy = array.clone()
Quicksort.sort(array)
array `should equal` arrayCopy.sortedArray()
}
@Test
fun testSortBy() {
val array = arrayOf(5, 2, 1, 4, 3, 23, 12, 1, 89, 7)
val arrayCopy = array.clone()
Quicksort.sortBy(array) { it }
array `should equal` arrayCopy.sortedArray()
}
@Test
fun testSortWith() {
val array = arrayOf(4, 5, 3, 2, 1)
val arrayCopy = array.clone()
Quicksort.sortWith(array) { t1, t2 ->
t1.compareTo(t2)
}
array `should equal` arrayCopy.sortedArray()
}
}