Advertisement
Telor_Goreng0

nomor 1 pertemuan 2

Apr 4th, 2021
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. package Pertemuan2;
  2. import java.util.Scanner;
  3. public class nomor1 {
  4. public static void main(String[] args)
  5. {
  6.  
  7. Scanner scan = new Scanner(System.in);
  8.  
  9. System.out.print("Masukkan jumlah Data : ");
  10. int jumlahdata = scan.nextInt();
  11.  
  12. int[] data = new int[jumlahdata];
  13. System.out.println();
  14. for(int a = 0; a < jumlahdata; a++)
  15. {
  16. System.out.print("Nilai Data ke-"+(a+1)+" : ");
  17. data[a] = scan.nextInt();
  18. }
  19.  
  20. System.out.println("\nData Sebelum di Sorting");
  21. for(int a = 0; a < jumlahdata; a++)
  22. System.out.print(data[a]+" ");
  23.  
  24. // Proses Bubble Sort
  25. System.out.println("\nProses Bubble Sort");
  26. for(int a = 0; a < jumlahdata; a++)
  27. {
  28. System.out.println("Iterasi ke-"+(a+1)+" :");
  29. for(int b = 0; b < jumlahdata; b++)
  30. System.out.print(data[b]+" ");
  31.  
  32. System.out.println(" Bandingkan "+data[0]+" dengan "+data[1]);
  33. for(int b = 0; b < jumlahdata-1; b++)
  34. {
  35. String pesan = " Tidak ada pertukaran";
  36. if(data[b] > data[b+1])
  37. {
  38.  
  39. pesan = " Data "+data[b]+" ditukar dengan "+data[b+1];
  40. int temp = data[b];
  41. data[b] = data[b+1];
  42. data[b+1] = temp;
  43.  
  44. }
  45.  
  46. if(b < jumlahdata-(a+1))
  47. {
  48. for(int c = 0; c < jumlahdata; c++)
  49. System.out.print(data[c]+" ");
  50.  
  51. System.out.println(pesan);;
  52. }
  53. }
  54.  
  55. System.out.println("\n");
  56. }
  57. System.out.print("Data Setelah di Sorting : ");
  58. for(int a = 0; a < jumlahdata; a++)
  59. System.out.print(data[a]+" ");
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement