Bubble Sort Method (Metode Gelembung)


Oke, pada kesempatan kali ini saya mau membahas tentang sorting pada array yaitu Bubble Sort atau bisa juga disebut metode gelembung. Agak terdengar aneh ya? saya rasa juga begitu :p

Sebelumnya pernah membuat program pencarian data dari elemen elemen array?  Jika pernah, terasa lambatkah proses pencariannya? Salah satu faktor yang menyebabkan lambatnya proses mungkin disebabkan gara gara program tersebut tidak dalam keadaan terurut. Berarti yang harus kita lakukan selanjutnya yaitu mengurutkan array tersebut.

Banyak sekali metode yang bisa digunakan, salah satunya yaitu Bubble Sort. Selain Bubble Sort ada banyak lagi metode, yaitu metode sisipan (insertion sort), maksimum-minimum (maximum-minimum sort), quick sort. Sebenarnya masih ada lagi, namun yang saya tahu hanya itu saja :p

Sekarang kita fokus ke algoritma Bubble Sort ya.

Step-by-step example.
Let us take the array of numbers "5 1 4 2 8", and sort the array from lowest number to greatest number using bubble sort algorithm. In each step, elements written in bold are being compared. Three passes will be required.

First Pass:
( 5 1 4 2 8 ) -> ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps them.
( 1 5 4 2 8 ) -> ( 1 4 5 2 8 ), Swap since 5 > 4
( 1 4 5 2 8 ) -> ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 ) -> ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does not swap them.
Second Pass:
( 1 4 2 5 8 ) -> ( 1 4 2 5 8 )
( 1 4 2 5 8 ) -> ( 1 2 4 5 8 ), Swap since 4 > 2
( 1 2 4 5 8 ) -> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) -> ( 1 2 4 5 8 )
Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm needs one whole pass without any swap to know it is sorted.
Third Pass:
( 1 2 4 5 8 ) -> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) -> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) -> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) -> ( 1 2 4 5 8 )

Ini source code nya menggunakan bahasa C++.

#include <iostream>
using namespace std;

#define MAX 5

void TampilkanArray(int A[], int n) {
        for (int j=0; j<n; j++) {
                cout << "A[" << j << "] = " << A[j] << endl;
        }
}

int main() {
        int A[MAX] = {5,1,4,2,8};
        int j, k;

        int temp;

        cout << "Sebelum pengurutan: " << endl;
        TampilkanArray(A, MAX);

        for (j=0; j<MAX-1; j++) {
                for (k=MAX-1; k>=(j+1); k--) {
                        if (A[k] < A[k-1]) {
                                temp = A[k];
                                A[k] = A[k-1];
                                A[k-1] = temp;
                        }
                }
        }
        cout << endl;

        cout << "Setelah pengurutan: " << endl;
        TampilkanArray(A, MAX);
        return 0;
}

Referensi :
Pemrograman C dan Implementasinya, Informatika.


Comments

  1. Replies
    1. sorting nya dua elemen ya.. ada tugas itu nis?

      Delete
    2. iya jy. kl sorting dua elemen aku kurang ngerti ya soalnya dia penghitungan waktu pk rumus gt jy

      Delete

Post a Comment

Popular posts from this blog

Apa Itu Kaskus

3 Tingkatan Cinta dan Berbagai Bentuk Cinta

Apa itu Desk Checking? Pengecekan Meja-kah?