Thursday, 24 December 2015

Write a program that input an array of n element and sort it in ascending order using bubble sort in c++

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{ int i,j,temp,n;
    cout<<"Enter the size of array";
cin>>n;
int a[n];
cout<<"Enter the elements of array"<<endl;
for(i=0;i<n;i++)
     {
     cin>>a[i];}              
    for(i=0;i<n;i++)
    {
     for(j=0;j<n;j++)
    {
     if(a[i]<a[j])
    {
     temp = a[i];
     a[i] = a[j];
a[j] = temp;}
    }
    }cout<<"Array in ascending order"<<endl;
     for(i=0;i<n;i++)
     cout<<a[i]<<endl;
getch();
}

5 comments: