73 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			Kotlin
		
	
	
	
	
	
			
		
		
	
	
			73 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			Kotlin
		
	
	
	
	
	
/*
 | 
						|
    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<Int>): List<Int> {
 | 
						|
    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()
 | 
						|
} |