• November 18th, 2011, 02:44 AM
izzahmed
The Question is :
Three stacks can be used to sort a list of numbers. Assuming stack in holds the input list of numbers, stack out is to hold the output list after sorting the numbers and temp is used during the sorting process. The sorting algorithm follows.

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

Code :

``` import java.util.*; public class MainAssignment3 { public static void main(String[]args) { int Max =0; int Value =0;   LinkedList<Integer>Input=new LinkedList<Integer>(); LinkedList<Integer>Temp=new LinkedList<Integer>(); LinkedList<Integer>OutPut=new LinkedList<Integer>();   Input.addLast(90); Input.addLast(21); Input.addLast(33); Input.addLast(80); Input.addLast(67);   System.out.println("The Input Stack is : " + Input);   while(!Input.isEmpty()) { Max = Input.removeLast(); Value = Input.removeLast();   System.out.println("MAx: " +Max); System.out.println("Value: " +Value);   if (Value > Max) { Temp.push(Max); Max=Value;   } else { Temp.push(Value);   }   Input=Temp; OutPut.push(Max); Temp.clear();   } System.out.println("The Output Stack is: " + OutPut);   } }```

can any one guide me how to sort a number ?
beause the OutPut Stack Should be [21, 33, 67, 80 ,90]
But i Only Get [80], any one can guide how to do ? thanks
• November 18th, 2011, 03:25 AM
jcloud
Why do you assign Temp to Input, leading to a cycle of only 1
• November 18th, 2011, 03:43 AM
izzahmed
it's just we have to use it any how so tell me a way to correct this..
• November 18th, 2011, 03:51 AM
Mr.777
jCloud has referenced you to the exact point. And also, where do you place the last remaining values?
• November 18th, 2011, 03:53 AM
Mr.777
Quote:

Originally Posted by izzahmed
it's just we have to use it any how so tell me a way to correct this..

NO!!!!
You try to find it, a little brain storming will lead you to the solution. Don't be dependent. Think on your own and you will get it done.
• November 18th, 2011, 03:06 PM
izzahmed
thanks Mr.777 got so many things to brain storming.. and found this is the best way. anyway thanks for the advice given..
• November 18th, 2011, 06:42 PM
izzahmed
and i would very much appreciate if u could just point the wrong part and rewrite the whole corrected code for me.

thanks
• November 18th, 2011, 11:37 PM
Mr.777