Advertisement
wewgu

Bubble Sort

Oct 12th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.46 KB | None | 0 0
  1. package bubblesort;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class BubbleSort
  6. {
  7.   public static void main(String[]args)
  8.   {
  9.     Scanner scanner = new Scanner(System.in);
  10.     int temp = 0;
  11.    
  12.     System.out.print("Masukkan banyak angka : ");
  13.     int jmlAngka = scanner.nextInt();
  14.     System.out.print("Masukkan angka : ");
  15.     int[] angka = new int[jmlAngka];
  16.  
  17.     for(int i=0;i<jmlAngka;i++)
  18.     {
  19.       angka[i] = scanner.nextInt();
  20.     }
  21.  
  22.     System.out.print("Angka sebelum diurutkan: ");
  23.  
  24.     for(int x:angka)
  25.     {
  26.       System.out.print(x + " ");
  27.     }
  28.  
  29.     System.out.println();
  30.  
  31.     for(int i=0;i<angka.length;i++)
  32.     {
  33.       for(int j=0;j<angka.length-1;j++)
  34.       {
  35.         if(angka[j] > angka[j+1])
  36.         {
  37.           temp = angka[j];
  38.           angka[j] = angka[j+1];
  39.           angka[j+1] = temp;
  40.         }
  41.       }
  42.     }
  43.  
  44.     System.out.println();
  45.     System.out.println("--- ASCENDING ---");
  46.    
  47.     for(int x:angka)
  48.     {
  49.       System.out.print(x + " ");
  50.     }
  51.  
  52.     System.out.println();
  53.  
  54.     for(int i=0;i<angka.length;i++)
  55.     {
  56.       for(int j=0;j<angka.length-1;j++)
  57.       {
  58.         if(angka[j] < angka[j+1])
  59.         {
  60.           temp = angka[j];
  61.           angka[j] = angka[j+1];
  62.           angka[j+1] = temp;
  63.         }
  64.       }
  65.     }
  66.  
  67.     System.out.println();
  68.     System.out.println("--- DESCENDING ---");
  69.  
  70.     for(int x:angka)
  71.     {
  72.       System.out.print(x + " ");
  73.     }
  74.  
  75.     System.out.println();
  76.   }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement