Simulasi Bubble Sort

mail@pastecode.io avatar
unknown
java
2 years ago
3.0 kB
14
Indexable
Never
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

public class Simulasi {
    public static void main(String[] args) throws IOException {
        System.out.println("Selamat Datang di Program Simulasi");
        System.out.println("Menu");
        System.out.println("1. Random Data");
        System.out.println("2. Simulasi Buble Sort - Ascending");
        System.out.println("3. Simulasi Selection Sort - Ascending");
        System.out.println("4. Simulasi Buble Sort - Descending");
        System.out.println("5. Simulasi Selection Sort - Descending");
        System.out.println("6. Keluar");
        System.out.println("Masukkan Pilihan Anda");

        Scanner in = new Scanner(System.in);

        int pilihan = in.nextInt();

        switch (pilihan) {
            case 1:
                random(in);
                break;
            case 2:
                bubbleSortAsc();
                break;
            case 6:
                System.out.println("Program berakhir");
                System.exit(0);
                break;
            default:
                System.out.println("Pilihan tidak tersedia");
                System.out.println("Program berakhir");
                System.exit(0);
        }
    }

    static void random(Scanner in) {
        System.out.print("Batas Bawah = ");
        int batasBawah = in.nextInt();
        System.out.print("Batas Atas = ");
        int batasAtas = in.nextInt();

        System.out.print(getRandomNumber(batasBawah, batasAtas)+" ");
        System.out.print(getRandomNumber(batasBawah, batasAtas)+" ");
        System.out.print(getRandomNumber(batasBawah, batasAtas)+" ");
        System.out.print(getRandomNumber(batasBawah, batasAtas)+" ");
        System.out.print(getRandomNumber(batasBawah, batasAtas)+" ");
    }

    static int getRandomNumber(int min, int max) {
        return (int) ((Math.random() * (max - min)) + min);
    }

    static void bubbleSortAsc() {
        int[] arr = new int[]{237, 84, 122, 260, 169};
        int n = arr.length;
//        for (int i = 0; i < n; i++) {
//            System.out.print(arr[i] + " ");
//        }
//        System.out.println();

        for (int i = 0; i < n - 1; i++) {
            System.out.println("Pass "+(i+1));
            for (int j = 0; j < n - i - 1; j++) {
                for (int k = 0; k < n; k++) {
                    System.out.print(arr[k] + " ");
                }
                System.out.println();
                if (arr[j] > arr[j + 1]) {
                    // swap arr[j+1] and arr[j]
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }

            System.out.println("Result of pass "+(i+1));
            for (int k = 0; k < n; k++) {
                System.out.print(arr[k] + " ");
            }
            System.out.println();

        }
    }
}