Use a tree stack to sort number in java?



Share on Google+Share on Google+

Use a tree stack to sort number in java?
0 Answer(s)      5 years and 5 months ago
Posted in : Java Beginners

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 temp.push(max) max = value 2.2.3 else temp.push(value) 2.3 in = temp 2.4 out.push(max) 2.5 temp.clear

3 print sorted stack

this my coding

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)

  } else {


 Input = Temp;

} System.out.println("The Output Stack is: " + OutPut);

} }

the answer i get is

The Input Stack is : [90, 21, 33, 80, 67] MAx: 67 Value: 80

The Output Stack is: [80]

i want to get the sorted stack in Output stack

View Answers

Java Spring Hibernate Struts Training ClassNotFoundException HttpRequestInterceptor java.lang.noclassdeffounderror: org/apache/http/httprequest noclassdeffounderror: org/apache/http/client/methods/httpurirequest java.lang.NoClassDefFoundError: org/apache/http/client/HttpClient How do I resolve this Java Class not found exception? httpclient java.lang.NoClassDefFoundError Apache Commons ioutils maven dependency Read/Convert an inputStream to a String What is the meaning of Java Platform? Why Java is a platform independent language? What is the benefits of learning Core Java? Which technology should I learn after Java? What is array in java with example? How to Convert ArrayList to Array? How to substring in Java? How to format number in Java? What is instance variable in Java? How to download MySQL JDBC driver? What is Calendar class in Java? Which is the best Java tutorials for beginners? How to rename a file in Java? How to delete file in Java code? How to get day from date in Java using Calendar? How to get day of week in Java? How to calculate Date Difference in Java? How to compare date in Java? How to declare array in Java? How to calculate average of array in Java? What is Array in Java? write a java program to find the summation of all the integers entered on command line Sum of two numbers using command line arguments in Java How to create and use Array in Java? How to pass command line arguments in Java? How to create Applet Hello World? Appending String efficiently in Java How to append String in Java? How to list even numbers between 1 and 100? How to add BigDecimal in Java? What is Abstraction In Java? Which is best Beginners Java Tutorial? What is java.util package? Create list from array in Java Filter collection in Java 8 What is the best way to filter a Java Collection? Easy way to transform Collection to Array? How to convert Collection to Array in Java? What are Basic Java Language Elements? Advanced Java Tutorials in 2017 Java brief history Best Reasons to learn Java


Advertisement null