Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
|
|
@ -0,0 +1,27 @@
|
|||
fun pancakeSort(a: IntArray) {
|
||||
/** Returns the index of the highest number in the range 0 until n. */
|
||||
fun indexOfMax(n: Int): Int = (0 until n).maxByOrNull{ a[it] }!!
|
||||
|
||||
/** Flips the elements in the range 0 .. n. */
|
||||
fun flip(index: Int) {
|
||||
a.reverse(0, index + 1)
|
||||
}
|
||||
|
||||
for (n in a.size downTo 2) { // successively reduce size of array by 1
|
||||
val index = indexOfMax(n) // find index of largest
|
||||
if (index != n - 1) { // if it's not already at the end
|
||||
if (index > 0) { // if it's not already at the beginning
|
||||
flip(index) // move largest to beginning
|
||||
println("${a.contentToString()} after flipping first ${index + 1}")
|
||||
}
|
||||
flip(n - 1) // move largest to end
|
||||
println("${a.contentToString()} after flipping first $n")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun main() {
|
||||
val a = intArrayOf(7, 6, 9, 2, 4, 8, 1, 3, 5)
|
||||
println("${a.contentToString()} initially")
|
||||
pancakeSort(a)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue