Code Competitions

Coding competition

Tuesday, 28 May 2013

perform sorting using bubble sort

#include<iostream.h>
#include<conio.h>
void display(int a[],int n)
{
    for(int i=0;i<n;i++)
    cout<<"\t"<<a[i];
}
void main()
{
    clrscr();
    int n,i,j,a[30];
    cout<<"enter the array size ";
    cin>>n;
    cout<<"\nenter the elements ";
    for(i=0;i<n;i++)
    cin>>a[i];
    cout<<"\nentered list is ";
    display(a,n);
    cout<<"\n\n\n\n\t\t\t\tBOOBLE SORT ";
    cout<<"\n\n";
    int temp;
    for(i=0;i<n-1;i++)
    {
        for(j=0;j<n-i-1;j++)
        {
            if(a[j]>a[j+1])
            {
                temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;
            }
        }
        cout<<"\nTHE LIST AFTER PASS "<<i+1<<"is\t";
        display(a,n);
    }
    cout<<"\n\n\nTHE FINAL SORTED LIST IS ";
    display(a,n);
    getch();
}
OUTPUT:

No comments:

Post a Comment