Created
April 5, 2022 05:11
-
-
Save HaradaKumiko/18378dfda92895d8f791fe89fd08d95a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BubbleSort { | |
//fungsi main untuk menjalankan kode | |
public static void main(String[] args) { | |
//ini array angka | |
int listAngka[] = {14,33,27,35,10}; | |
//bikin objek untuk menggunakan fungsi yang ada pada class, nama objek kali ini ob1 | |
BubbleSort ob1 = new BubbleSort(); | |
//panggil fungsi sorting dengan parameter array angka | |
ob1.sortingGelembung(listAngka); | |
System.out.println("Sorting gelembung ascending: "); | |
//foreach array | |
ob1.printArray(listAngka); | |
} | |
void printArray(int listAngka[]){ | |
for (int l = 0; l < listAngka.length; l++){ | |
System.out.print(listAngka[l] + " "); | |
} | |
} | |
void sortingGelembung(int listAngka[]){ | |
//untuk mengetahui jumlah element pada array | |
int panjang = listAngka.length; | |
//iterasi sebanyak n-1, dimana n adalah jumlah element | |
for (int i = 0; i < panjang-1; i++){ | |
//iterasi sebanyak n-1, dimana n adalah jumlah element | |
for (int j = 0; j < panjang-1; j++ ){ | |
//Jika element setelahnya lebih kecil, maka tukar posisinya | |
if (listAngka[j] < listAngka[j+1]){ | |
int temporary = listAngka[j]; | |
listAngka[j] = listAngka[j+1]; | |
listAngka[j+1] = temporary; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment