BAHASA PEMROGRAMAN 1 : PENGURUTAN DAN TRACING SELECTION SORT (GENAP)
SOURCE CODE :
package javaapplication9;
import java.util.Scanner;
public class PengurutanDataSelectionSort {
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
String menu[] = new String[5];
int harga[] = new int[5];
int pilih=0;
menu[0] = "Nasi Goreng "; harga[0] = 15000;
menu[1] = "Mie Ayam "; harga[1] = 12000;
menu[2] = "Empal Gentong"; harga[2] = 20000;
menu[3] = "Ketoprak"; harga[3] = 10000;
menu[4] = "Bubur Ayam"; harga[4] = 14000;
do{//menu
System.out.println("\n----MENU WARUNG----");
System.out.println("1.Daftar Makanan");
System.out.println("2.Urutan Termahal");
System.out.println("3.Selesai");
System.out.print("Pilih = ");
pilih = sc.nextInt();
switch(pilih){
case 1:
System.out.println("MENU \tHARGA");
for (int i=0; i<5; i++){
System.out.println(menu[i]+"\t"+harga[i]);
}
break;
case 2:
String menu2[] = new String[5];
menu2 = menu;
int harga2[] = new int[5];
menu2 = menu;
harga2 = harga;
for (int i = 0; i < harga2.length - 1; i++) {
for (int j = i+1; j < harga2.length; j++) {
if(harga2[i] < harga2[j]){
int temp = harga2[j];
harga2[j] = harga2[i];
harga2[i] = temp;
String s = menu2[j];
menu2[j] = menu2[i];
menu2[i] = s;
}
}
}
System.out.println("---HARGA TERMAHAL---");
System.out.println("MENU \tHARGA");
for (int i=0; i<5; i++){
System.out.println(menu2[i]+"\t"+harga2[i]);
}
break;
case 3:
System.out.println("Terima kasih...");
break;
}
}while(pilih!=3);
}
}
NETBEANS
OUTPUT
TRACING TABLE
Harga |
i |
j |
harga2[i]<harga2[j] |
S |
[16000] [14000] [24000] [12000] [15000] |
0 |
1 |
16000 < 14000 ➡️ F |
|
|
|
2 |
16000 < 24000 ➡️ T |
24000 |
[24000] [14000] [16000] [12000] [15000] |
|
3 |
24000 < 12000 ➡️ F |
|
|
|
4 |
24000 < 15000 ➡️ F |
|
|
|
5 |
|
|
|
1 |
2 |
14000 < 16000 ➡️ T |
16000 |
[24000] [16000] [14000] [12000] [15000] |
|
3 |
16000 < 12000 ➡️ F |
|
|
|
4 |
16000 < 15000 ➡️ F |
|
|
|
5 |
|
|
|
2 |
3 |
14000 < 12000 ➡️ F |
|
|
|
4 |
14000 < 15000 ➡️ T |
15000 |
[24000] [16000] [15000] [12000] [14000] |
|
5 |
|
|
|
3 |
4 |
12000 < 14000 ➡️ T |
14000 |
[24000] [16000] [15000] [14000] [12000] |
|
5 |
|
|
|
4 |
|
|
|
Comments
Post a Comment