Friday, August 31, 2012

C Program for Bubble Sort

#include<iostream.h>
#include<conio.h>
#include<alloc.h>
class array
{
  int **x,r,c,i,j;
  public:
  void sort(int**,int,int);
  void process();
};
void array::sort(int **x,int r,int c)
{
  int i,j,k,l,t;
  for(k=r-1;k>=0;k--)
  {
   for(l=c-1;l>=0;l--)
   {
    for(i=0;i<r;i++)
    {
     for(j=0;j<c;j++)
     {
       if(j==(c-1)&&i==(r-1))
       break;
       if(j==l&&i==k)
       break;
       if(j==(c-1)&&i<(r-1))
    {
     if(x[i][j]>x[i+1][0])
     {
        t=x[i][j];
        x[i][j]=x[i+1][0];
        x[i+1][0]=t;
      }
     }
       else
    {
     if(x[i][j]>x[i][j+1])
     {
        t=x[i][j];
        x[i][j]=x[i][j+1];
        x[i][j+1]=t;
     }
      }
    }
   }
  }
 }
}
void array::process()
{
cout<<"Enter Size of 2'D Array\n";
cin>>r>>c;
x=(int**)malloc((r*c)*2);
cout<<"--------------------------------------\n";
cout<<"Enter Elements of 2'D Array\n";
for(i=0;i<r;i++)
for(j=0;j<c;j++)
cin>>x[i][j];
sort(x,r,c);
cout<<"--------------------------------------\n";
cout<<"After Sorting \n";
for(i=0;i<r;i++)
{
 for(j=0;j<c;j++)
 cout<<"\t"<<x[i][j];
 cout<<endl;
}
cout<<"--------------------------------------\n";


}
void main()
{
clrscr();
array a;
cout<<"*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n";
cout<<"   -*- Program for Bubble Sorting For 2'D Array -*-\n";
cout<<"*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n";
a.process();
cout<<"--------------------------------------\n";
getch();
}

No comments:

Post a Comment