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