OOPC Practical 3


Practical : 3
Subject : Object Oriented Programming with C++

Aim : C++ Program to sort array using bubble sort

Source Code :
#include<iostream.h>
#include<conio.h>
void asc(int a[100],int n)
{
int i,j,temp;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
}
void des(int a[100],int n)
{
int i,j,temp;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]<a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
}
void main()
{
clrscr();
int i,n,a[100];
cout<<"\n enter n : ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"\n enter element : ";
cin>>a[i];
}
asc(a,n);
cout<<"\n sorted array";
for(i=0;i<n;i++)
{
cout<<"\n"<<a[i];
}
des(a,n);
cout<<"\n sorted array";
for(i=0;i<n;i++)
{
cout<<"\n"<<a[i];
}
getch();
}

Output :


Pratik Boghani

Author & Editor

Life is all about the next step.

0 comments:

Post a Comment