fun bubblesort(arr: Array<Int>) : Array<Int> {
var finished: Int = 0
var swapped: Int
var size: Int = arr.size - 1
while( finished == 0 ) {
swapped = 0
for(i in 0..size-1) {
if(arr[i] > arr[i+1]) {
val holder: Int = arr[i]
arr[i] = arr[i+1]
arr[i+1] = holder
swapped = 1
}
}
if(swapped == 1) size = size - 1
else
finished = 1
}
return arr
}
fun main(args: Array<String>) {
var integers = arrayOf(9,8,7,6,5,4,3,2,1)
println(integers.joinToString(" "))
integers = bubblesort(integers)
println(integers.joinToString(" "))
}
ZnVuIGJ1YmJsZXNvcnQoYXJyOiBBcnJheTxJbnQ+KSA6IEFycmF5PEludD4gewoJICAgICAgIAogICAgdmFyIGZpbmlzaGVkOiBJbnQgPSAwCgogICAgdmFyIHN3YXBwZWQ6IEludCAgIAoKICAgIHZhciBzaXplOiBJbnQgPSBhcnIuc2l6ZSAtIDEgICAgCgogICAgd2hpbGUoIGZpbmlzaGVkICA9PSAwICkgewoKICAgICAgICAgICBzd2FwcGVkID0gMAoKICAgICAgICAgICBmb3IoaSBpbiAwLi5zaXplLTEpIHsKCiAgICAgICAgICAgICAgIGlmKGFycltpXSA+IGFycltpKzFdKSB7CgogICAgICAgICAgICAgICAgICAgICB2YWwgaG9sZGVyOiBJbnQgPSBhcnJbaV0KCiAgICAgICAgICAgICAgICAgICAgICAgICBhcnJbaV0gPSBhcnJbaSsxXQoKICAgICAgICAgICAgICAgICAgICAgICAgIGFycltpKzFdID0gaG9sZGVyCiAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgICAgICAgIHN3YXBwZWQgPSAxICAgICAKICAgICAgICAgICAgICAgfSAgCiAgICAgICAgICAgfQoKICAgICAgICAgICBpZihzd2FwcGVkID09IDEpIHNpemUgPSBzaXplIC0gMQoKICAgICAgICAgICAgICAgICAgIGVsc2UgCiAgICAgICAgICAgICAgICAgICAgICAgICAgICBmaW5pc2hlZCA9IDEgCgogICAgfSAgCgogICAgcmV0dXJuIGFycgp9CgpmdW4gbWFpbihhcmdzOiBBcnJheTxTdHJpbmc+KSB7CgogICAgdmFyIGludGVnZXJzID0gYXJyYXlPZig5LDgsNyw2LDUsNCwzLDIsMSkgIAkKCiAgICBwcmludGxuKGludGVnZXJzLmpvaW5Ub1N0cmluZygiICIpKQoKICAgIGludGVnZXJzID0gYnViYmxlc29ydChpbnRlZ2VycykKCiAgICBwcmludGxuKGludGVnZXJzLmpvaW5Ub1N0cmluZygiICIpKQp9