134 lines
4.4 KiB
Kotlin
134 lines
4.4 KiB
Kotlin
package com.example.Kalkulator
|
|
|
|
import android.os.Bundle
|
|
import androidx.activity.ComponentActivity
|
|
import androidx.activity.compose.setContent
|
|
import androidx.compose.foundation.background
|
|
import androidx.compose.foundation.layout.Box
|
|
import androidx.compose.foundation.layout.Column
|
|
import androidx.compose.foundation.layout.Spacer
|
|
import androidx.compose.foundation.layout.fillMaxSize
|
|
import androidx.compose.foundation.layout.fillMaxWidth
|
|
import androidx.compose.foundation.layout.height
|
|
import androidx.compose.foundation.layout.padding
|
|
import androidx.compose.foundation.lazy.LazyColumn
|
|
import androidx.compose.foundation.lazy.items
|
|
import androidx.compose.material3.Button
|
|
import androidx.compose.material3.OutlinedTextField
|
|
import androidx.compose.material3.Scaffold
|
|
import androidx.compose.material3.Text
|
|
import androidx.compose.material3.TopAppBar
|
|
import androidx.compose.material3.TopAppBarDefaults
|
|
import androidx.compose.runtime.Composable
|
|
import androidx.compose.runtime.getValue
|
|
import androidx.compose.runtime.mutableStateOf
|
|
import androidx.compose.runtime.remember
|
|
import androidx.compose.runtime.setValue
|
|
import androidx.compose.ui.Alignment
|
|
import androidx.compose.ui.Modifier
|
|
import androidx.compose.ui.graphics.Color
|
|
import androidx.compose.ui.text.font.FontWeight
|
|
import androidx.compose.ui.unit.dp
|
|
import androidx.compose.ui.unit.sp
|
|
|
|
class MainActivity : ComponentActivity() {
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
super.onCreate(savedInstanceState)
|
|
setContent {
|
|
SelectionSortApp()
|
|
}
|
|
}
|
|
}
|
|
|
|
@Composable
|
|
fun SelectionSortApp() {
|
|
var inputText by remember { mutableStateOf("") }
|
|
var numbers by remember { mutableStateOf(listOf<Int>()) }
|
|
var sortedNumbers by remember { mutableStateOf(listOf<Int>()) }
|
|
|
|
fun selectionSort(list: List<Int>): List<Int> {
|
|
val arr = list.toMutableList()
|
|
for (i in 0 until arr.size - 1) {
|
|
var minIndex = i
|
|
for (j in i + 1 until arr.size) {
|
|
if (arr[j] < arr[minIndex]) {
|
|
minIndex = j
|
|
}
|
|
}
|
|
// Tukar posisi elemen
|
|
val temp = arr[i]
|
|
arr[i] = arr[minIndex]
|
|
arr[minIndex] = temp
|
|
}
|
|
return arr
|
|
}
|
|
|
|
Scaffold(
|
|
topBar = {
|
|
TopAppBar(
|
|
title = { Text("Selection Sort Demo") },
|
|
colors = TopAppBarDefaults.topAppBarColors(
|
|
containerColor = Color(0xFF1565C0),
|
|
titleContentColor = Color.White
|
|
)
|
|
)
|
|
}
|
|
) { padding ->
|
|
Column(
|
|
modifier = Modifier
|
|
.padding(padding)
|
|
.fillMaxSize()
|
|
.padding(16.dp),
|
|
horizontalAlignment = Alignment.CenterHorizontally
|
|
) {
|
|
Text(
|
|
"Masukkan angka dipisahkan dengan koma (contoh: 5,3,8,1,4)",
|
|
fontSize = 16.sp
|
|
)
|
|
|
|
Spacer(modifier = Modifier.height(10.dp))
|
|
|
|
OutlinedTextField(
|
|
value = inputText,
|
|
onValueChange = { inputText = it },
|
|
label = { Text("Input Angka") },
|
|
singleLine = true,
|
|
modifier = Modifier.fillMaxWidth()
|
|
)
|
|
|
|
Spacer(modifier = Modifier.height(16.dp))
|
|
|
|
Button(onClick = {
|
|
try {
|
|
numbers = inputText.split(",").map { it.trim().toInt() }
|
|
sortedNumbers = selectionSort(numbers)
|
|
} catch (e: Exception) {
|
|
sortedNumbers = emptyList()
|
|
}
|
|
}) {
|
|
Text("Urutkan (Selection Sort)")
|
|
}
|
|
|
|
Spacer(modifier = Modifier.height(20.dp))
|
|
|
|
if (sortedNumbers.isNotEmpty()) {
|
|
Text("Hasil Pengurutan:", fontWeight = FontWeight.Bold, fontSize = 18.sp)
|
|
Spacer(modifier = Modifier.height(8.dp))
|
|
LazyColumn {
|
|
items(sortedNumbers) { num ->
|
|
Box(
|
|
modifier = Modifier
|
|
.fillMaxWidth()
|
|
.padding(4.dp)
|
|
.background(Color(0xFFBBDEFB))
|
|
.padding(12.dp)
|
|
) {
|
|
Text(text = num.toString(), fontSize = 20.sp)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|