Code Competitions

Coding competition
Showing posts with label bubble sort. Show all posts
Showing posts with label bubble sort. Show all posts

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:

Sunday, 12 May 2013

Program to sort list of elements using bubble sort

#include<stdio.h>
#include<conio.h>
void main()
{
    int a[20],i=0,j=0,n;
    clrscr();
    printf("enter the number of elements");
    scanf("%d",&n);
    printf("enter the elements of list");
    for(i=0;i<=n-1;i++)
    {
        scanf("%d",&a[i]);
    }
    for(i=0;i<=n-1;i++)
    {
        for(j=0;j<=n-i-2;j++)
        {
            if(a[j]>a[j+1])
            {
                int temp;
                temp=a[j+1];
                a[j+1]=a[j];
                a[j]=temp;
            }
        }
    }
    printf("the sorted list is : ");
    for(j=0;j<n;j++)
    {
        printf("%d\t",a[j]);
    }
    getche();
}