Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Pertemuan2;
- import java.util.Scanner;
- public class nomor1 {
- public static void main(String[] args)
- {
- Scanner scan = new Scanner(System.in);
- System.out.print("Masukkan jumlah Data : ");
- int jumlahdata = scan.nextInt();
- int[] data = new int[jumlahdata];
- System.out.println();
- for(int a = 0; a < jumlahdata; a++)
- {
- System.out.print("Nilai Data ke-"+(a+1)+" : ");
- data[a] = scan.nextInt();
- }
- System.out.println("\nData Sebelum di Sorting");
- for(int a = 0; a < jumlahdata; a++)
- System.out.print(data[a]+" ");
- // Proses Bubble Sort
- System.out.println("\nProses Bubble Sort");
- for(int a = 0; a < jumlahdata; a++)
- {
- System.out.println("Iterasi ke-"+(a+1)+" :");
- for(int b = 0; b < jumlahdata; b++)
- System.out.print(data[b]+" ");
- System.out.println(" Bandingkan "+data[0]+" dengan "+data[1]);
- for(int b = 0; b < jumlahdata-1; b++)
- {
- String pesan = " Tidak ada pertukaran";
- if(data[b] > data[b+1])
- {
- pesan = " Data "+data[b]+" ditukar dengan "+data[b+1];
- int temp = data[b];
- data[b] = data[b+1];
- data[b+1] = temp;
- }
- if(b < jumlahdata-(a+1))
- {
- for(int c = 0; c < jumlahdata; c++)
- System.out.print(data[c]+" ");
- System.out.println(pesan);;
- }
- }
- System.out.println("\n");
- }
- System.out.print("Data Setelah di Sorting : ");
- for(int a = 0; a < jumlahdata; a++)
- System.out.print(data[a]+" ");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement