2
I have an algorithm that via console helps the user in sorting a list of items, it works well for the console (code below), but I’m adapting the interface to become an app (as print)
I wanted that as soon as the user clicked one of the two buttons, the function promptInput returned the text corresponding to the button, so that the second while of the insertionSort function worked well replacing the console readline.
override fun onCreateView() {
val arr = arrayOf("finish homework", "finish chores", "go shopping")
val result = insertionSort("What needs to be done first", arr)
println(result.toList())
}
fun promptInput(comparison: String, str1: String, str2: String): String {
println("$comparison: $str1 or $str2?");
return readLine()!!
}
fun insertionSort(comparison: String, arr: Array<String>): Array<String> {
println("arr size: ${arr.size}")
var len = arr.size
var i = -1
var j: Int
var tmp: String
while (len-- != 0) {
tmp = arr[++i];
j = i
while (j-- != 0 && (promptInput(comparison, arr[j], tmp) == arr[j])) {
arr[j + 1] = arr[j];
}
arr[j + 1] = tmp
}
return arr.apply { reverse() }
}