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 : Insertion Sort

#include <iostream>
using namespace std;

void insertionsort(int  *arr, int size)
{
    int i, j, temp;
    for(i = 1; i < size ; i++)
    {
        j = i;
        while( j > 0 && *(arr+j-1) > *(arr+j) )
        {
            temp = *(arr+j);
            *(arr+j) = *(arr+j-1);
            *(arr+j-1) = temp;
            j--;
        }
    }
}

void insertionsort2(int  *arr, int size)
{
    int i, j, temp;
    for(i = 1; i < size ; i++)
    {
        j = i;
        temp = *(arr+j);
        while( j > 0 && temp < *(arr+j-1) )
        {
            *(arr+j) = *(arr+j-1);
            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) << " ";
    }
    insertionsort2(arr, sizeof(arr)/sizeof(int));
    cout << "\nSetelah :" << endl;

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

 

I will continue about this algorithm later...
time to sleep :)

Share this post: | | | |
Posted: Oct 05 2007, 02:06 PM by sagi | with no comments
Filed under: ,

Comments

No Comments

Leave a Comment

(required) 

(required) 

(optional)

(required) 
Are you human?:  


Enter the numbers above: