Upload files to "/"
This commit is contained in:
		
							parent
							
								
									c39b193f08
								
							
						
					
					
						commit
						2914ecc4d8
					
				
							
								
								
									
										105
									
								
								MainActivitySorting.kt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										105
									
								
								MainActivitySorting.kt
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,105 @@
 | 
				
			|||||||
 | 
					package com.example.dendiyogiapratama_sorting
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					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.MaterialTheme
 | 
				
			||||||
 | 
					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 com.example.dendiyogiapratama_sorting.ui.theme.DendiYogiaPratamaSortingTheme
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class MainActivity : ComponentActivity() {
 | 
				
			||||||
 | 
					    override fun onCreate(savedInstanceState: Bundle?) {
 | 
				
			||||||
 | 
					        super.onCreate(savedInstanceState)
 | 
				
			||||||
 | 
					        enableEdgeToEdge()
 | 
				
			||||||
 | 
					        setContent {
 | 
				
			||||||
 | 
					            DendiYogiaPratamaSortingTheme {
 | 
				
			||||||
 | 
					                Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
 | 
				
			||||||
 | 
					                    val numbers = listOf(64, 34, 25, 12, 22, 11, 90)
 | 
				
			||||||
 | 
					                    val sortedNumbers = bubbleSortAngka(numbers)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    Greeting(
 | 
				
			||||||
 | 
					                        name = "AndroidDen",
 | 
				
			||||||
 | 
					                        numbers = numbers,
 | 
				
			||||||
 | 
					                        sortedNumbers = sortedNumbers,
 | 
				
			||||||
 | 
					                        modifier = Modifier.padding(innerPadding)
 | 
				
			||||||
 | 
					                    )
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Fungsi Bubble Sort - Algoritma sorting manual step by step
 | 
				
			||||||
 | 
					fun bubbleSortAngka(numbers: List<Int>): List<Int> {
 | 
				
			||||||
 | 
					    // Konversi List ke MutableList agar bisa diubah
 | 
				
			||||||
 | 
					    val arr = numbers.toMutableList()
 | 
				
			||||||
 | 
					    val n = arr.size
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Loop untuk setiap elemen
 | 
				
			||||||
 | 
					    for (i in 0 until n - 1) {
 | 
				
			||||||
 | 
					        // Loop untuk membandingkan elemen yang berdekatan
 | 
				
			||||||
 | 
					        for (j in 0 until n - i - 1) {
 | 
				
			||||||
 | 
					            // Jika elemen sebelah kiri lebih besar dari kanan, tukar posisinya
 | 
				
			||||||
 | 
					            if (arr[j] > arr[j + 1]) {
 | 
				
			||||||
 | 
					                // Proses penukaran (swap)
 | 
				
			||||||
 | 
					                val temp = arr[j]
 | 
				
			||||||
 | 
					                arr[j] = arr[j + 1]
 | 
				
			||||||
 | 
					                arr[j + 1] = temp
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return arr
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@Composable
 | 
				
			||||||
 | 
					fun Greeting(
 | 
				
			||||||
 | 
					    name: String,
 | 
				
			||||||
 | 
					    numbers: List<Int>,
 | 
				
			||||||
 | 
					    sortedNumbers: List<Int>,
 | 
				
			||||||
 | 
					    modifier: Modifier = Modifier
 | 
				
			||||||
 | 
					) {
 | 
				
			||||||
 | 
					    Column(modifier = modifier.padding(16.dp)) {
 | 
				
			||||||
 | 
					        Text(
 | 
				
			||||||
 | 
					            text = "Hello $name!",
 | 
				
			||||||
 | 
					            style = MaterialTheme.typography.headlineMedium,
 | 
				
			||||||
 | 
					            modifier = Modifier.padding(bottom = 8.dp)
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        Text(
 | 
				
			||||||
 | 
					            text = "Angka asli: ${numbers.joinToString(", ")}",
 | 
				
			||||||
 | 
					            style = MaterialTheme.typography.bodyLarge,
 | 
				
			||||||
 | 
					            modifier = Modifier.padding(bottom = 4.dp)
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        Text(
 | 
				
			||||||
 | 
					            text = "Angka yang terurut (Bubble Sort): ${sortedNumbers.joinToString(", ")}",
 | 
				
			||||||
 | 
					            style = MaterialTheme.typography.bodyLarge,
 | 
				
			||||||
 | 
					            color = MaterialTheme.colorScheme.primary
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@Preview(showBackground = true)
 | 
				
			||||||
 | 
					@Composable
 | 
				
			||||||
 | 
					fun GreetingPreview() {
 | 
				
			||||||
 | 
					    val numbers = listOf(64, 34, 25, 12, 22, 11, 90)
 | 
				
			||||||
 | 
					    val sortedNumbers = bubbleSortAngka(numbers)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    DendiYogiaPratamaSortingTheme {
 | 
				
			||||||
 | 
					        Greeting(
 | 
				
			||||||
 | 
					            name = "AndroidDen",
 | 
				
			||||||
 | 
					            numbers = numbers,
 | 
				
			||||||
 | 
					            sortedNumbers = sortedNumbers
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user