66 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Kotlin
		
	
	
	
	
	
			
		
		
	
	
			66 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Kotlin
		
	
	
	
	
	
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(5, 1, 4, 2, 8),
 | 
						|
                        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) {
 | 
						|
    val sorted = bubbleSort(data)
 | 
						|
    Text(
 | 
						|
        text = "Data sebelum sort: ${data.joinToString(", ")}\n" +
 | 
						|
                "Data setelah sort: ${sorted.joinToString(", ")}",
 | 
						|
        modifier = modifier
 | 
						|
    )
 | 
						|
}
 | 
						|
 | 
						|
@Preview(showBackground = true)
 | 
						|
@Composable
 | 
						|
fun GreetingPreview() {
 | 
						|
    ProgramsortingTheme {
 | 
						|
        Tampilan(intArrayOf(5, 1, 4, 2, 8))
 | 
						|
    }
 | 
						|
}
 |