71 lines
2.0 KiB
Kotlin
71 lines
2.0 KiB
Kotlin
package com.example.programsorting
|
|
|
|
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.programsorting.ui.theme.ProgramsortingTheme
|
|
|
|
/* Nama: Yosep Gamaliel Mulia
|
|
NPM : 202310715105
|
|
*/
|
|
|
|
class MainActivity : ComponentActivity() {
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
super.onCreate(savedInstanceState)
|
|
enableEdgeToEdge()
|
|
setContent {
|
|
ProgramsortingTheme {
|
|
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
|
|
Tampilan(
|
|
data = intArrayOf(20, 21, 22, 100, 3),
|
|
modifier = Modifier.padding(innerPadding)
|
|
)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Fungsi Bubble Sort
|
|
fun bubbleSort(numbers: IntArray): IntArray {
|
|
val arr = numbers.copyOf()
|
|
val n = arr.size
|
|
for (i in 0 until n - 1) {
|
|
for (j in 0 until n - i - 1) {
|
|
if (arr[j] > arr[j + 1]) {
|
|
// Swap
|
|
val temp = arr[j]
|
|
arr[j] = arr[j + 1]
|
|
arr[j + 1] = temp
|
|
}
|
|
}
|
|
}
|
|
return arr
|
|
}
|
|
|
|
@Composable
|
|
fun Tampilan(data: IntArray, modifier: Modifier = Modifier) {
|
|
val sorted = bubbleSort(data)
|
|
Text(
|
|
text = "Data sebelum sort: ${data.joinToString(", ")}\n" +
|
|
"Data setelah sort: ${sorted.joinToString(", ")}",
|
|
modifier = modifier
|
|
)
|
|
}
|
|
|
|
@Preview(showBackground = true)
|
|
@Composable
|
|
fun TampilanPreview() {
|
|
ProgramsortingTheme {
|
|
Tampilan(intArrayOf(20, 21, 22, 100, 3))
|
|
}
|
|
}
|