Rabu, 20 Juni 2012

Source Code Sorting Data dengan metode BUBBLE SORT

Ada beberapa metode serching data, tapi kali ini saya akan membagi source code dan flow char sorting data dengan menggunakan bubble sort.

BBerikut flowcart dasar dari metode searching dengan menggunakan bubble sort :

Oke, kemudian gimana implementasinya dalam program java ?
Berikut contoh programnya (Source Code)




import java.util.Scanner;

public class BubbleSort_Cek_dari_kanan {
    public static void main(String[] args) {
        Scanner scanKata = new Scanner(System.in);
        Scanner scanAngka = new Scanner(System.in);
        System.out.print("Masukkan Jumlah Angka : ");
        int input = scanAngka.nextInt();
        String [] tInput = new String [input];
        int mtdSort = 0;
        System.out.println("\nCatatan :\n"
                + "Inputan Boleh Berupa Angka Atau Kata\n");
        for (int i = 0; i < input; i++) {
            System.out.print("Masukkan inputan ke-"+(i+1)+" : ");
            tInput[i] = scanKata.nextLine();
        }
        do {
            System.out.print("\nPilih Metode Sorting :\n"
                    + "1. Ascending\n"
                    + "2. Descending\n\n"
                    + "Masukkan Pilihan Anda : ");
            try {
                String mtd = scanKata.next();
                mtdSort = Integer.parseInt(mtd);
            } catch (Exception e) {
                mtdSort = 3;
            }
            System.out.println("");
            switch (mtdSort) {
                case 1:
                    int count = 1;
                    System.out.println("# Data Awal : ");
                    for (int i = 0; i < input; i++) {
                        if (i == input-1) {
                            System.out.println(tInput[i]+"\n");
                        } else {
                            System.out.print(tInput[i]+"    ");
                        }
                    }
                    for (int x = 0; x < input-1; x++) {
                        for (int y = input-1; y > 0; y--) {
                            if (tInput[y].compareToIgnoreCase(tInput[y-1])<0) {
                                String temp = tInput[y];
                                tInput[y] = tInput[y-1];
                                tInput[y-1] = temp;
                            }
                        }
                        if (count < input-1) {
                            System.out.println("# Proses ke-"+count+" :");
                        } else if (count == input-1) {
                            System.out.println("# Hasil Sorting :");
                        }
                        for (int i = 0; i < input; i++) {
                            if (i == input-1) {
                                System.out.println(tInput[i]+"\n");
                            } else {
                                System.out.print(tInput[i]+"    ");
                            }
                        }
                        count++;
                    }
                    break;
                case 2:
                    count = 1;
                    System.out.println("# Data Awal : ");
                    for (int i = 0; i < input; i++) {
                        if (i == input-1) {
                            System.out.println(tInput[i]+"\n");
                        } else {
                            System.out.print(tInput[i]+"    ");
                        }
                    }
                    for (int x = 0; x < input-1; x++) {
                        for (int y = input-1; y > 0; y--) {
                            if (tInput[y].compareToIgnoreCase(tInput[y-1])>0) {
                                String temp = tInput[y];
                                tInput[y] = tInput[y-1];
                                tInput[y-1] = temp;
                            }
                        }
                        if (count < input-1) {
                            System.out.println("# Proses ke-"+count+" :");
                        } else if (count == input-1) {
                            System.out.println("# Hasil Sorting :");
                        }
                        for (int i = 0; i < input; i++) {
                            if (i == input-1) {
                                System.out.println(tInput[i]+"\n");
                            } else {
                                System.out.print(tInput[i]+"    ");
                            }
                        }
                        count++;
                    }
                    break;
                default:
                    System.out.println("\nMasukkan Angka 1 - 2 !!!");
                    break;
            }
        } while (mtdSort == 3);
    }
}




0 komentar:

Karna hidup punya banyak rasa untuk harimu