Tugas 3 Android Studio
Tugas Android Studio(3) Membuat Sort Pengurutan
This commit is contained in:
		
						commit
						4f7de56607
					
				
							
								
								
									
										83
									
								
								MainActivity.kt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										83
									
								
								MainActivity.kt
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,83 @@
 | 
				
			|||||||
 | 
					package com.example.selectionsort
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					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.foundation.rememberScrollState
 | 
				
			||||||
 | 
					import androidx.compose.foundation.verticalScroll
 | 
				
			||||||
 | 
					import androidx.compose.material3.MaterialTheme
 | 
				
			||||||
 | 
					import androidx.compose.material3.Scaffold
 | 
				
			||||||
 | 
					import androidx.compose.material3.Text
 | 
				
			||||||
 | 
					import androidx.compose.runtime.Composable
 | 
				
			||||||
 | 
					import androidx.compose.runtime.remember
 | 
				
			||||||
 | 
					import androidx.compose.ui.Modifier
 | 
				
			||||||
 | 
					import androidx.compose.ui.tooling.preview.Preview
 | 
				
			||||||
 | 
					import androidx.compose.ui.unit.dp
 | 
				
			||||||
 | 
					import com.example.selectionsort.ui.theme.SelectionSortTheme
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class MainActivity : ComponentActivity() {
 | 
				
			||||||
 | 
					    override fun onCreate(savedInstanceState: Bundle?) {
 | 
				
			||||||
 | 
					        super.onCreate(savedInstanceState)
 | 
				
			||||||
 | 
					        enableEdgeToEdge()
 | 
				
			||||||
 | 
					        setContent {
 | 
				
			||||||
 | 
					            SelectionSortTheme {
 | 
				
			||||||
 | 
					                Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
 | 
				
			||||||
 | 
					                    SelectionSortScreen(modifier = Modifier.padding(innerPadding))
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@Composable
 | 
				
			||||||
 | 
					fun SelectionSortScreen(modifier: Modifier = Modifier) {
 | 
				
			||||||
 | 
					    val numbers = remember { (1..40).toList() }
 | 
				
			||||||
 | 
					    val shuffledNumbers = remember { numbers.shuffled() }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    val sortedNumbers = remember {
 | 
				
			||||||
 | 
					        shuffledNumbers.toMutableList().apply {
 | 
				
			||||||
 | 
					            for (i in 0 until size - 1) {
 | 
				
			||||||
 | 
					                var minIndex = i
 | 
				
			||||||
 | 
					                for (j in i + 1 until size) {
 | 
				
			||||||
 | 
					                    if (this[j] < this[minIndex]) {
 | 
				
			||||||
 | 
					                        minIndex = j
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					                if (minIndex != i) {
 | 
				
			||||||
 | 
					                    val temp = this[i]
 | 
				
			||||||
 | 
					                    this[i] = this[minIndex]
 | 
				
			||||||
 | 
					                    this[minIndex] = temp
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    Column(
 | 
				
			||||||
 | 
					        modifier = modifier
 | 
				
			||||||
 | 
					            .padding(16.dp)
 | 
				
			||||||
 | 
					            .verticalScroll(rememberScrollState())
 | 
				
			||||||
 | 
					    ) {
 | 
				
			||||||
 | 
					        Text(text = "Angka sebelum di urut (acak):")
 | 
				
			||||||
 | 
					        Text(text = shuffledNumbers.joinToString(", "))
 | 
				
			||||||
 | 
					        Text(text = "")
 | 
				
			||||||
 | 
					        Text(text = "Angka sudah di urut (Selection Sort):")
 | 
				
			||||||
 | 
					        Text(text = sortedNumbers.joinToString(", "))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        Text(text = "")
 | 
				
			||||||
 | 
					        Text(text = "Penjelasan Selection Sort:")
 | 
				
			||||||
 | 
					        Text(text = "Selection Sort bekerja dengan cara mencari angka terkecil dari daftar yang belum diurutkan,")
 | 
				
			||||||
 | 
					        Text(text = "kemudian menukarnya dengan angka paling kiri yang belum diurutkan. Proses ini diulang terus sampai seluruh angka terurut dari yang terkecil ke terbesar.")
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@Preview(showBackground = true)
 | 
				
			||||||
 | 
					@Composable
 | 
				
			||||||
 | 
					fun SelectionSortScreenPreview() {
 | 
				
			||||||
 | 
					    SelectionSortTheme {
 | 
				
			||||||
 | 
					        SelectionSortScreen()
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user