The Notes of Sagi Arsyad

EveryMy Paint is Black, Don't ask me to draw Rainbow
See also: Other Geeks@INDC

News

ASP.NET

My Organization

Algorithm : Bubble Sort

#include <iostream>
using namespace std;

void bubblesort(int  *arr, int size)
{
    int i, j, temp;
    
    for(i = 0 ; i < size ; i++)
    {
        for(j = i+1 ; j < size ; j++)
        {
            if(*(arr + i) > *(arr + j))
            {
                temp = *(arr + i);
                *(arr + i) = *(arr + j);
                *(arr + j) = temp;
            }
        }
    }
}

int main()
{
    int arr[] = {3, 6, 1, -4, 5, 2, 9, -1, 7, 0, 2};
    int i;
    cout << "\nSebelum :" << endl;

    for(i = 0 ; i < sizeof(arr)/sizeof(int) ; i++)
    {
        cout << *(arr + i) << " ";
    }
    
    bubblesort(arr, sizeof(arr)/sizeof(int));
    
    cout << "\nSetelah :" << endl;

    for(i = 0 ; i < sizeof(arr)/sizeof(int) ; i++)
    {
        cout << *(arr + i) << " ";
    }
    
    return 0;
}

 

analisa algorithm

    Bubble Sort merupakan implementasi algoritma sorting yang paling mudah namun tidak disarankan dipakai pada data (array) dalam jumlah besar. hal ini dikarenakan laju pertumbuhan (notasi Big-O) dari Bubble Sort adalah O(n2)

Share this post: | | | |
Posted: Oct 04 2007, 02:18 AM by sagi | with no comments
Filed under: ,

Comments

No Comments

Leave a Comment

(required) 

(required) 

(optional)

(required) 
Are you human?:  


Enter the numbers above: