Tuesday, March 11, 2014

BUBBLE SORT using STACK in C++

#include<iostream>
using namespace std;
class stack1
{public:
int top1=-1,a[10],s=10;
void push1(int d);
int pop1();
};
void stack1::push1(int d)
{if(top1>=s-1)
cout<<"stack1 is full\n";
else
a[++top1]=d;
}
int stack1::pop1()
{int c;
if(top1<=-1)
{
cout<<"stack1 is empty\n";
return 0;
}
else
{ c =a[top1--];

return c;
}}class stack2
{public:
int top2=-1,s=10,a[10];
void push2(int d);
int pop2();
};

void stack2::push2(int d)
{if(top2>=s-1)
cout<<"stack2 is full\n";
else
a[++top2]=d;
}
int stack2::pop2()
{int e;
if(top2<=-1)
{
cout<<"stack2 is empty\n";
return 0;
}
else
{ e =a[top2--];

return e;
}}
int main()
{stack1 s1;
stack2 s2;
 int i,j,n,a,b,m,;
cout<<"enter ";
for(i=0;i<4;i++)
{cin>>n;
s1.push1(n);
}
for(i=3;i>0;i--)
{
for(j=i;j>0;j--)
{a=s1.pop1();
b=s1.pop1();
if(a>=b)
{s2.push2(a);
s1.push1(b);
}
else
{s2.push2(b);
s1.push1(a);
}
}
while(s2.top2>-1)
{n=s2.pop2();
s1.push1(n);
    }
}
while(s1.top1>-1)
cout<<s1.pop1();
return 0;
}

No comments:

Post a Comment

Contributors

Translate