Tugas - Program Sorting
This commit is contained in:
parent
ffb742192a
commit
776ea03e31
66
Tugas2/MainActivity.kt
Normal file
66
Tugas2/MainActivity.kt
Normal file
@ -0,0 +1,66 @@
|
||||
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
|
||||
|
||||
class MainActivity : ComponentActivity() {
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
enableEdgeToEdge()
|
||||
setContent {
|
||||
ProgramsortingTheme {
|
||||
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
|
||||
Tampilan(
|
||||
data = intArrayOf(12, 2, 5, 15, 20),
|
||||
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(12, 2, 5, 15, 20))
|
||||
}
|
||||
}
|
||||
BIN
Tugas2/Screenshot 2025-11-07 165013.png
Normal file
BIN
Tugas2/Screenshot 2025-11-07 165013.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 8.4 KiB |
Loading…
x
Reference in New Issue
Block a user