
Patrick B. answered 10/27/19
Math and computer tutor/teacher
Here is a quick algorithm
minBottom ( StackType & stack)
{
//local temporary stacks
StackType tempStack;
StackType stackMins; //contains only the mins
StackType stackTemp;
Stack myStack(stack); //calls the copy constructor
int minValue = 32767;
//first pass through the stack determines the min value
while (!myStack.IsEmpty())
{
StackObject = myStack.Pop();
int iNum = Obj2Int(StackObject);
if (iNum<minValue)
{
minValue=iNum;
};
tempStack.Push(StackObject);
}
//tempStack contains 4,2,8,5,2,3,4 and minValue=2
//second pass through the stack puts the min. ints into a separate stack
while (!tempStack.IsEmpty())
{
StackObject =tempStack.Pop();
int iNum = Obj2Int(StackObject);
if (iNum==minValue)
{
stackMins.Push( StackObject);
}
else
{
stackTemp.Push(StackObject);
}
}
//stackTemp contains 4,3,5,8,4 and stackMins contains 2,2; tempStack is empty
//Step 3: moves them out of stackTemp and back into tempStack so they reverse
while (!stackTemp.IsEmpty())
{
StackObject stackObj = stackTemp.Pop();
tempStack.Push(stackObj);
}
//Last step: puts the mins on the final stack first; then the others...
Stack stackReturn;
while (!stackMins.IsEmpty())
{
stackReturn.Push( stackMins.Pop())
}
while (!tempStack.IsEmpty())
{
stackReturn.Push(tempStack.Pop());
}
return(stackReturn);
} //minBottom();