Wednesday, 7 January 2015

Contoh script java bubble sorting dengan larik


import java.util.Scanner;
public class bubble
{
                public void bubbleSort(int larik2[])
                {
                                for(int i=0; i<larik2.length; i++)
                                {
                                                for(int elemen=0; elemen<larik2.length-1;elemen++)
                                                {
                                                                if(larik2[elemen]>larik2[elemen+1])
                                                                tukar(larik2,elemen,elemen+1);
                                                }
                                }
                }             
                public void tukar(int larik3[],int satu,int dua)
                {
                 int temp;
                 temp = larik3[satu];
                 larik3[satu]=larik3[dua];
                 larik3[dua] = temp;       
                }
                public static void main(String args[])
                {
                                Scanner masuk=new Scanner(System.in);
                                bubble lrk = new bubble();
                                int nilai[]=new int[5];
                                System.out.println("masukkan 5 data nilai");
                                for (int i=0; i<5; i++)
                                {
                                                System.out.print((i+1)+" :");
                                                nilai[i]= masuk.nextInt();             
                                }
                                                System.out.println("Data nilai yang dimasukkan");
                                                for(int i=0; i<5; i++)
                                                System.out.println(nilai[i]);
                                                System.out.println("Data hasil pengurutan");
                                                lrk.bubbleSort(nilai);
                                                for (int i=0; i<5; i++)
                                                System.out.println(nilai[i]);        
                                                               
                                }

                }

Download script Link1 / Link2

No comments:

Post a Comment