Search Your Query

Custom Search

Tuesday, 24 May 2011

Bubble Sort by Pointers Programe


#include <iostream.h>

void swap (int *num1, int *num2)
{
    int t=*num1;
    *num1 = *num2;
    *num2 = t;
}

void main()
{
    int num[10];
    //int t;

    cout << "\t\t-:Sorting in Ascending Order:-" << endl << endl;
    cout << "Enter any 10 numbers to sort them in ascending order: " << endl;

    for ( int l=0 ; l < 10 ; l++ )
    {
        cin >> num[l];
    }

    for ( int i=10 ; i > 0 ; i-- )
    {
        for ( int j=0 ; j < i ; j++ )
        {
            if ( num[j] > num[j+1] )
            {
                swap(num+j , num+j+1);
            }
        }
    }
    cout << endl << "Ascending Order is : ";
    for ( int k=0 ; k < 10 ; k++ )
    {
        cout << num[k] << " < ";
    }
    cout << "\b\b\b ." << endl;
}

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...