This commit is contained in:
parent
a9bd9df609
commit
079723937d
@ -57,16 +57,8 @@ class BinaryHeap<T> private constructor(
|
|||||||
return result.toOption()
|
return result.toOption()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun print() {
|
|
||||||
print("|")
|
|
||||||
array.forEach {
|
|
||||||
print(" $it |")
|
|
||||||
}
|
|
||||||
println()
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun peek(): Option<T> = if (size >= 1)
|
override fun peek(): Option<T> = if (size >= 1)
|
||||||
array[1].toOption()
|
array[FIRST_ELEMENT].toOption()
|
||||||
else
|
else
|
||||||
None
|
None
|
||||||
|
|
||||||
@ -84,7 +76,6 @@ class BinaryHeap<T> private constructor(
|
|||||||
exchange(array, k / WAY, k)
|
exchange(array, k / WAY, k)
|
||||||
pushUp(array, k / WAY)
|
pushUp(array, k / WAY)
|
||||||
}
|
}
|
||||||
else -> return
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -109,9 +100,8 @@ class BinaryHeap<T> private constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun exchange(array: Array<T?>, i: Int, j: Int) {
|
private fun exchange(array: Array<T?>, i: Int, j: Int) {
|
||||||
array[i] = array[j].also {
|
array[i] = array[j]
|
||||||
array[j] = array[i]
|
.also { array[j] = array[i] }
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun isFull(): Boolean = size + 1 == array.size
|
private fun isFull(): Boolean = size + 1 == array.size
|
||||||
|
@ -60,8 +60,8 @@ object OrderedListSorter {
|
|||||||
|
|
||||||
fun <T : Comparable<T>> sort(vararg lists: List<T>): List<T> {
|
fun <T : Comparable<T>> sort(vararg lists: List<T>): List<T> {
|
||||||
val heap = BinaryHeap.createMinPriorityQueue<Node<T>, T> { it.value }
|
val heap = BinaryHeap.createMinPriorityQueue<Node<T>, T> { it.value }
|
||||||
lists.forEachIndexed { index, list ->
|
lists.forEachIndexed { k, list ->
|
||||||
heap.insert(Node(list.first(), index, 0))
|
heap.insert(Node(list.first(), k, 0))
|
||||||
}
|
}
|
||||||
|
|
||||||
val outList = ArrayList<T>()
|
val outList = ArrayList<T>()
|
||||||
|
Loading…
Reference in New Issue
Block a user