-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstackSort.java
More file actions
29 lines (29 loc) · 913 Bytes
/
stackSort.java
File metadata and controls
29 lines (29 loc) · 913 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
26
27
28
29
package assignment;
import java.util.*;
class stackSort {
public static Stack<Integer> s(Stack<Integer> input) {
Stack<Integer> tempStack = new Stack<Integer>();
while(!input.isEmpty()){
int temp = input.pop();
while(!tempStack.isEmpty() && tempStack.peek() > temp) {
input.push(tempStack.pop());
}
tempStack.push(temp);
}
return tempStack;
}
public static void main(String args[]) {
Stack<Integer> input = new Stack<Integer>();
input.add(34);
input.add(3);
input.add(31);
input.add(98);
input.add(92);
input.add(23);
Stack<Integer> tempStack = s(input);
System.out.println("Sorted numbers are:");
while (!tempStack.empty()) {
System.out.print(tempStack.pop()+" ");
}
}
}