Upload Tugas ProgramSorting
This commit is contained in:
parent
1ea080fcba
commit
17621df31f
77
ProgramSorting/MainActivity.kt
Normal file
77
ProgramSorting/MainActivity.kt
Normal file
@ -0,0 +1,77 @@
|
||||
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.Column
|
||||
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 androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
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(15, 20, 35, 50, 85),
|
||||
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]) {
|
||||
val temp = arr[j]
|
||||
arr[j] = arr[j + 1]
|
||||
arr[j + 1] = temp
|
||||
}
|
||||
}
|
||||
}
|
||||
return arr
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun Tampilan(data: IntArray, modifier: Modifier = Modifier) {
|
||||
|
||||
// Acak angka dulu biar berbeda
|
||||
val shuffledData = data.copyOf()
|
||||
shuffledData.shuffle()
|
||||
|
||||
// Lalu sort
|
||||
val sorted = bubbleSort(shuffledData)
|
||||
|
||||
Column(modifier = modifier.padding(16.dp)) {
|
||||
Text(text = "Data sebelum sort (acak):", fontSize = 18.sp)
|
||||
Text(text = shuffledData.joinToString(", "), fontSize = 18.sp)
|
||||
|
||||
Text(text = "\nData setelah sort:", fontSize = 18.sp)
|
||||
Text(text = sorted.joinToString(", "), fontSize = 18.sp)
|
||||
}
|
||||
}
|
||||
|
||||
@Preview(showBackground = true)
|
||||
@Composable
|
||||
fun TampilanPreview() {
|
||||
ProgramsortingTheme {
|
||||
Tampilan(intArrayOf(15, 20, 35, 50, 85))
|
||||
}
|
||||
}
|
||||
BIN
ProgramSorting/Screenshot 2025-11-05 192819.png
Normal file
BIN
ProgramSorting/Screenshot 2025-11-05 192819.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 6.8 KiB |
Loading…
x
Reference in New Issue
Block a user