Anyone can help me with this algorithm?
1 set up stack in and print it
2 while stack in is not empty repeat
2.1 max = in.pop
2.2 while there are still element in stack in repeat
2.2.1 value = in.pop
2.2.2 if value > max
2.2.2.1 temp.push(max)
2.2.2.2 max = value
2.2.3 else
2.2.3.1 temp.push(value)
2.3 in = temp
2.4 out.push(max)
2.5 temp.clear
3 print sorted stack
Below is my code but it does not working....
#include <iostream>
#include <stack>
using namespace std;
int main ()
{
stack <int> in,out,temp;
int num[] = {50,10,60,40,80,20,70};
cout<<"Unsorted numbers in stack in :"<<endl;
for (int i=0; i<7; i++)
{
in.push(num[i]);
cout<<in.top()<<endl; //print stack in
}
cout << endl;
while (!in.empty())
{
int max=in.top();
while (!in.empty())
{
in.pop();
int value=in.top();
if (value > max)
{
temp.push(max);
max=value;
}
else
{
temp.push(value);
}
}
in = temp;
out.push(max);
// temp->clear();
}
cout<<"Sorted numbers in stack out :";
for (int i=0; i<7; i++)
{
cout<<out.top()<<endl; //print stack in
out.pop();
}
return 0;
}