Pages

Powered by Blogger.

Friday, January 16, 2015

Mengurutkan bilangan menggunakan Bubble Sort dengan java

Source code mengurutkan bilangan menggunakan Bubble Sort (Algoritma dan Struktur Data)

Kode program:

class BubbleSort {

    BubbleSort(int[] x) {

    }

    BubbleSort() {
       
    }

  
   public void BubbleSort(int[] arr){
     for(int i=0; i<arr.length; i++){
        for(int j=1; j<arr.length; j++){
            if(arr[j]< arr[j-1] ){
                int temp = arr[j];
                arr[j] = arr[j-1];
                arr[j-1] = temp;
            }
        }
     }

     for(int i=0; i<arr.length; i++)
     {
         System.out.print(arr[i] + " ");
     }
    }

}


public class Main {


    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        Scanner input = new Scanner(System.in);


        System.out.print("Enter the size of the array: ");
        int n = input.nextInt();
        int[] x = new int[n];

        System.out.print("Enter "+ n +" numbers: ");
        for(int i=0; i<n; i++)
        {
            x[i] = input.nextInt();
        }

        BubbleSort access = new  BubbleSort();
    System.out.print("The Sorted numbers: ");
        access.BubbleSort(x);
    }

}

No comments:

Post a Comment

 

Blogger news

Blogroll

About