Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package bubblesort;
- import java.util.Scanner;
- public class BubbleSort
- {
- public static void main(String[]args)
- {
- Scanner scanner = new Scanner(System.in);
- int temp = 0;
- System.out.print("Masukkan banyak angka : ");
- int jmlAngka = scanner.nextInt();
- System.out.print("Masukkan angka : ");
- int[] angka = new int[jmlAngka];
- for(int i=0;i<jmlAngka;i++)
- {
- angka[i] = scanner.nextInt();
- }
- System.out.print("Angka sebelum diurutkan: ");
- for(int x:angka)
- {
- System.out.print(x + " ");
- }
- System.out.println();
- for(int i=0;i<angka.length;i++)
- {
- for(int j=0;j<angka.length-1;j++)
- {
- if(angka[j] > angka[j+1])
- {
- temp = angka[j];
- angka[j] = angka[j+1];
- angka[j+1] = temp;
- }
- }
- }
- System.out.println();
- System.out.println("--- ASCENDING ---");
- for(int x:angka)
- {
- System.out.print(x + " ");
- }
- System.out.println();
- for(int i=0;i<angka.length;i++)
- {
- for(int j=0;j<angka.length-1;j++)
- {
- if(angka[j] < angka[j+1])
- {
- temp = angka[j];
- angka[j] = angka[j+1];
- angka[j+1] = temp;
- }
- }
- }
- System.out.println();
- System.out.println("--- DESCENDING ---");
- for(int x:angka)
- {
- System.out.print(x + " ");
- }
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement