diff --git a/MainActivity.kt b/MainActivity.kt new file mode 100644 index 0000000..da07202 --- /dev/null +++ b/MainActivity.kt @@ -0,0 +1,73 @@ +/* + Nama: Rafi Fattan Fitriardi + NPM : 202310715002 + */ + + +package com.example.sorting + +import android.os.Bundle +import androidx.activity.ComponentActivity +import androidx.activity.compose.setContent +import androidx.compose.foundation.layout.* +import androidx.compose.material3.* +import androidx.compose.runtime.* +import androidx.compose.ui.Modifier +import androidx.compose.ui.tooling.preview.Preview +import androidx.compose.ui.unit.dp + +class MainActivity : ComponentActivity() { + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContent { + BubbleSortApp() + } + } +} + +@Composable +fun BubbleSortApp() { + // Array awal + val original = listOf(8, 3, 5, 2, 9) + val sorted = bubbleSort(original.toMutableList()) + + // Tampilan UI + Column( + modifier = Modifier + .fillMaxSize() + .padding(16.dp) + ) { + Text( + text = "Algoritma Bubble Sort", + style = MaterialTheme.typography.headlineSmall + ) + Spacer(modifier = Modifier.height(16.dp)) + + Text("Sebelum Sorting: ${original.joinToString()}") + Spacer(modifier = Modifier.height(8.dp)) + Text("Sesudah Sorting: ${sorted.joinToString()}") + } +} + + +fun bubbleSort(arr: MutableList): List { + 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]) { + // Tukar elemen + val temp = arr[j] + arr[j] = arr[j + 1] + arr[j + 1] = temp + } + } + } + return arr +} + + +@Preview(showBackground = true) +@Composable +fun PreviewBubbleSortApp() { + BubbleSortApp() +} \ No newline at end of file diff --git a/Tugas 2.PNG b/Tugas 2.PNG new file mode 100644 index 0000000..f489b20 Binary files /dev/null and b/Tugas 2.PNG differ