Upload files to "Tugas-2_Sorting"
This commit is contained in:
parent
a6ef226b2a
commit
777f9620f2
70
Tugas-2_Sorting/MainActivity.kt
Normal file
70
Tugas-2_Sorting/MainActivity.kt
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
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))
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
Tugas-2_Sorting/Tugas-2_Sorting.png
Normal file
BIN
Tugas-2_Sorting/Tugas-2_Sorting.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 281 KiB |
Loading…
x
Reference in New Issue
Block a user