71 lines
2.1 KiB
Kotlin
71 lines
2.1 KiB
Kotlin
package com.example.array
|
|
|
|
import android.os.Bundle
|
|
import androidx.activity.ComponentActivity
|
|
import androidx.activity.compose.setContent
|
|
import androidx.activity.enableEdgeToEdge
|
|
import androidx.compose.foundation.layout.fillMaxSize
|
|
import androidx.compose.foundation.layout.padding
|
|
import androidx.compose.material3.Scaffold
|
|
import androidx.compose.material3.Text
|
|
import androidx.compose.runtime.Composable
|
|
import androidx.compose.ui.Modifier
|
|
import androidx.compose.ui.tooling.preview.Preview
|
|
import com.example.array.ui.theme.ArrayTheme
|
|
|
|
class MainActivity : ComponentActivity() {
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
super.onCreate(savedInstanceState)
|
|
enableEdgeToEdge()
|
|
setContent {
|
|
ArrayTheme {
|
|
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
|
|
|
|
// Data awal
|
|
val data = arrayOf(19, 15, 1, 14, 3)
|
|
val before = data.joinToString(", ")
|
|
|
|
// Jalankan insertion sort
|
|
insertionSort(data)
|
|
val after = data.joinToString(", ")
|
|
|
|
Greeting(
|
|
text = "Array sebelum: $before\nArray sesudah: $after",
|
|
modifier = Modifier.padding(innerPadding)
|
|
)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Fungsi insertion sort
|
|
private fun insertionSort(array: Array<Int>) {
|
|
for (i in 1 until array.size) {
|
|
val key = array[i]
|
|
var j = i - 1
|
|
|
|
// Pindahkan elemen yang lebih besar dari key ke satu posisi di depan
|
|
while (j >= 0 && array[j] > key) {
|
|
array[j + 1] = array[j]
|
|
j--
|
|
}
|
|
array[j + 1] = key
|
|
}
|
|
}
|
|
}
|
|
|
|
@Composable
|
|
fun Greeting(text: String, modifier: Modifier = Modifier) {
|
|
Text(
|
|
text = text,
|
|
modifier = modifier
|
|
)
|
|
}
|
|
|
|
@Preview(showBackground = true)
|
|
@Composable
|
|
fun GreetingPreview() {
|
|
ArrayTheme {
|
|
Greeting("Array sebelum: 9, 5, 1, 4, 3\nArray sesudah: 1, 3, 4, 5, 9")
|
|
}
|
|
} |