Upload files to "/"
This commit is contained in:
		
						commit
						afb529bb62
					
				
							
								
								
									
										26
									
								
								bubblesort.kt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								bubblesort.kt
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,26 @@
 | 
				
			|||||||
 | 
					fun main() {
 | 
				
			||||||
 | 
					    // Data awal
 | 
				
			||||||
 | 
					    val numbers = arrayOf(64, 34, 25, 12, 22, 11, 90)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    println("Sebelum Sorting: ${numbers.joinToString(", ")}")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Panggil fungsi bubbleSort
 | 
				
			||||||
 | 
					    bubbleSort(numbers)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    println("Sesudah Sorting: ${numbers.joinToString(", ")}")
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Fungsi Bubble Sort
 | 
				
			||||||
 | 
					fun bubbleSort(arr: Array<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 posisi
 | 
				
			||||||
 | 
					                val temp = arr[j]
 | 
				
			||||||
 | 
					                arr[j] = arr[j + 1]
 | 
				
			||||||
 | 
					                arr[j + 1] = temp
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user