forked from Jasmine-21/Java-FOP
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSortstack.java
More file actions
25 lines (23 loc) · 822 Bytes
/
Sortstack.java
File metadata and controls
25 lines (23 loc) · 822 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.util.*;
public class Sortstack {
public static void main(String args[]) {
Stack<Integer> stack = new Stack<>();
Scanner s = new Scanner(System.in);
int n = s.nextInt();
while (n-- > 0)
stack.push(s.nextInt());
sort(stack);
}
static void sort(Stack<Integer> stack) {
Stack<Integer> temp = new Stack<>();
while (!stack.isEmpty()) {
// Remove the top element from the original stack
int n = stack.pop();
// Remove the elements form temp stack which are greater than n and push into original stack
while (!temp.isEmpty() && temp.peek() > n)
stack.push(temp.pop());
temp.push(n);
}
System.out.println(temp);
}
}