Java Program To Sort Stack Using Temporary Stack

Chapter: Interview Programs Last Updated: 12-07-2016 04:39:35 UTC

Program:

            /* ............... START ............... */
                
import java.util.Stack;

public class JavaSortStack {
	public static Stack<Integer> sortStack(Stack<Integer> input) {

		Stack<Integer> tmpStack = new Stack<Integer>();
		while (!input.isEmpty()) {
			int tmp = input.pop();
			System.out.println("Element taken out: " + tmp);
			while (!tmpStack.isEmpty() && tmpStack.peek() > tmp) {
				input.push(tmpStack.pop());
			}
			tmpStack.push(tmp);
			System.out.println("input: " + input);
			System.out.println("tmpStack: " + tmpStack);
		}
		return tmpStack;
	}

	public static void main(String a[]) {

		Stack<Integer> input = new Stack<Integer>();
		input.add(20);
		input.add(3);
		input.add(4);
		input.add(76);
		input.add(65);
		input.add(73);
		System.out.println("input: " + input);
		System.out.println("final sorted list: " + sortStack(input));
	}
}
                /* ............... END ............... */
        

Output

input: [20, 3, 4, 76, 65, 73]
Element taken out: 73
input: [20, 3, 4, 76, 65]
tmpStack: [73]
Element taken out: 65
input: [20, 3, 4, 76, 73]
tmpStack: [65]
Element taken out: 73
input: [20, 3, 4, 76]
tmpStack: [65, 73]
Element taken out: 76
input: [20, 3, 4]
tmpStack: [65, 73, 76]
Element taken out: 4
input: [20, 3, 76, 73, 65]
tmpStack: [4]
Element taken out: 65
input: [20, 3, 76, 73]
tmpStack: [4, 65]
Element taken out: 73
input: [20, 3, 76]
tmpStack: [4, 65, 73]
Element taken out: 76
input: [20, 3]
tmpStack: [4, 65, 73, 76]
Element taken out: 3
input: [20, 76, 73, 65, 4]
tmpStack: [3]
Element taken out: 4
input: [20, 76, 73, 65]
tmpStack: [3, 4]
Element taken out: 65
input: [20, 76, 73]
tmpStack: [3, 4, 65]
Element taken out: 73
input: [20, 76]
tmpStack: [3, 4, 65, 73]
Element taken out: 76
input: [20]
tmpStack: [3, 4, 65, 73, 76]
Element taken out: 20
input: [76, 73, 65]
tmpStack: [3, 4, 20]
Element taken out: 65
input: [76, 73]
tmpStack: [3, 4, 20, 65]
Element taken out: 73
input: [76]
tmpStack: [3, 4, 20, 65, 73]
Element taken out: 76
input: []
tmpStack: [3, 4, 20, 65, 73, 76]
final sorted list: [3, 4, 20, 65, 73, 76]

Tags

Sort A Stack Using Temporary Stack, Java

Similar Programs Chapter Last Updated
Java Program To Find Frequency Of Character In String Interview Programs 28-09-2017
Java Program To Find Power Of Number Using While Loop Interview Programs 30-08-2017
Java Program To Count Divisors Of Integer Number Interview Programs 24-06-2017
Java Program To Sort N Names In Ascending Order Interview Programs 24-06-2017
Java Program To Count Total Number Of Words In String Interview Programs 24-06-2017
Java Program To Print All Prime Numbers From 1 to N Interview Programs 24-06-2017
Java Program To Extract Digits / Numbers From String Interview Programs 22-09-2018
Java First Repeated Character In String Interview Programs 16-05-2017
Java String Character Repetition Count Interview Programs 15-05-2017
Java Program To Check Vowel Or Not Interview Programs 25-09-2018
Java Program To Check Alphabet Or Not Interview Programs 06-04-2017
Java Program To Find First Repeated And Non Repeated Character In String Interview Programs 25-03-2017
Java Spiral Matrix Interview Programs 22-09-2018
Java Program To Reverse A Number Using Strings Interview Programs 13-02-2017
Java Program To Print Diamond Star Pattern Interview Programs 16-12-2016
Java Program To Print Pyramid Pattern Of Star Interview Programs 16-12-2016
Java Program To Find Second Largest Number In Array Interview Programs 04-12-2016
Java Depth First Search Interview Programs 04-12-2016
Java Breadth First Search Interview Programs 04-12-2016
Java Linked List Length Recursive Solution Interview Programs 17-11-2016
Java Linked List Length Iterative Solution Interview Programs 17-11-2016
Java Linked List Node Deletion At Given Position Interview Programs 17-11-2016
Java Linked List Node Delete Interview Programs 17-11-2016
Java Sum Of Digits Using Recursion Interview Programs 06-11-2016
Java Program To Reverse Vowels Of String Interview Programs 05-11-2016
Java Program To Remove Vowels From String Interview Programs 05-11-2016
Java Find Top Two Maximum Numbers In Array Interview Programs 05-11-2016
Java QuickSort Example Interview Programs 05-11-2016
Java Binary Tree Spiral Level Traversal Interview Programs 04-11-2016
Java Binary Tree Preorder Traversal Interview Programs 04-11-2016

1 2 3 4 5