# include <iostream.h>
# include<conio.h>
# define TRUE 1
#define FALSE 0
#define MAX 20
void bubble(int x[ ], int n)
{
int hold, j, pass;
int switched = TRUE;
for(pass=0; pass<n-1&&switched==TRUE; pass++)
{ // Outer loop controls the number of passes
switched = FALSE;
// Initially no interchange have been made on this pass
for(j=0; j<n-pass-1; j++)
// Inner loop governs each individual pass
if (x[j] > x[j+1])
{
//Interchange elements if they are out of order
switched = TRUE;
hold=x[j];
x[j] = x[j+1];
x[j+1] = hold;
} //end if
} // end for
} // end bubble function
void main( )
{
int i, n;
int x[MAX];
cout<<"\n Enter numbers of elements :";
cin>>n;
cout<<"\n Enter Numbers in any order : \n";
for(i=0; i<n; i++)
{
cout<<"\n Element["<<i<<"] = ";
cin>>x[i];
}
bubble(x,n);
cout<<"\n \nSorted Elements are : \n";
for(i=0; i<n; i++)
cout<<"\n Element["<<i<<"] = "<<x[i];
// Displays the largest and 2nd largest element from sorted list
cout<<"\n\n Largest Element is : "<<x[n-1];
cout<<"\n Second largest Element is : "<<x[n-2];
getch( );
}
0 comments:
Post a Comment