Write a Program of Bubble Sorting Algorithm in C++.


BUBBLE  SORT

#include<iostream>
using namespace std;
void sort(int a[5])
{
            int i,j,last=4,count;
            for(i=0;i<last;i++)
            {
                        count=0;
                        for(j=0;j<last-1;j++)
                        {
                                    if(a[j]>a[j+1])
                                    {
                                                swap(a[j],a[j+1]);
                                                count++;
                                    }
                        }
                        if(count==0)
                        {
                                    break;
                        }
            }
            cout<<"\nSorted Array\n";
            for(i=0;i<5;i++)
            {
                        cout<<"\n"<<a[i];
            }
}
void swap(int &i,int &j)
{
            i=i+j;
            j=i-j;
            i=i-j;
}
int main()
{
            int a[5],i;
            cout<<"\nEnter elements of array:";
            for(i=0;i<5;i++)
            {
                        cin>>a[i];
            }
            sort(a);
}
OUTPUT




No comments:

Post a Comment