u

Wednesday 6 June 2012

C++ Program implementing Bubble Sort for an entered range of numbers. Using passing of array for sorting through functions

C++ Program implementing Bubble Sort for an entered range of numbers. Using passing of array for sorting through functions


#include <iostream.h>
#include <conio.h>
int bubbleInput(int n);
double bubbleSort(double a[], int n);
double bubblePass(double a[], int n);
double bubbleSwap(double& i, double& j);
double bubbleOutput(double a[], int n);
int main ()


    {
    int n;
    clrscr();
    cout<<"Please do not exceed above 100."<<endl;
    cout<<"How many numbers would you like to sort: ";
    cin>>n;
    bubbleInput(n);
    cout<<endl;
    return 0;
}
int bubbleInput(int n)


    {
    double a[100];
    for (int i=0; i<n; i++)


        {
            cout<<"Please enter a number to sort: ";
            cin>>a[i];
        }
        bubbleSort(a, n);
        bubbleOutput(a, n);
        return 0;
    }
    double bubbleSort(double a[], int n)


    {
        for (int i=0; i<n; i++)
        bubblePass(a,n);
        return 0;
    }
    double bubblePass(double a[], int n)


    {
        for (int i=0; i<n-1; i++)
        if (a[i] > a[i+1])
        bubbleSwap(a[i], a[i+1]);
        return 0;
    }
    double bubbleSwap(double& i, double& j)


    {
        double temp = i;
        i = j;
        j = temp;
        return 0;
    }
    double bubbleOutput(double a[], int n)


    {


        for (int i=0; i<n; i++) {
            cout << a[i] << " ";
        }
        cout<<" "<<endl;
        cout<<"Press any key to continue"<<endl;
        getch();
        return 0;
        }

0 comments:

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More