Saturday, June 13, 2015

C++ program to implement Bubble Sort

/*-------------------------------------------------------------------------------------------------------------
File Name:      bubblesort.cpp
Author:           sujith
Program:        Write a C++ program to implement Bubble Sort
Date:               23-04-2015
--------------------------------------------------------------------------------------------------------------*/
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
        int n,i,j,a[20],temp;
        cout<<"Enter the total number of element\n";
        cin>>n;
        cout<<"Enter the elements\n";
        for(i=0;i<n;i++)
        {
                cin>>a[i];
        }
        cout<<"sorted element is\n";
        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;
                        }
                }
        }
        for(i=0;i<n;i++){
                cout<<a[i]<<"\n";
        }
}
OUTPUT:

[141740@localhost ~]$ g++ bubblesort.cpp
[141740@localhost ~]$ ./a.out
Enter the total number of element
5
Enter the elements
56        23        5          4          9
sorted element is
4
5
9
23
56
[141740@localhost ~]$ g++ bubblesort.cpp
[141740@localhost ~]$ ./a.out
Enter the total number of element
5
Enter the elements
5          3          2          9          7
sorted element is
2
3
5
7
9
[141740@localhost ~]$



No comments:

Post a Comment