-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSorting_stack.java
More file actions
executable file
·36 lines (33 loc) · 876 Bytes
/
Copy pathSorting_stack.java
File metadata and controls
executable file
·36 lines (33 loc) · 876 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
30
31
32
33
34
35
36
import java.util.*;
class Sorting_stack{
public static void main(String[] args){
Stack<Integer> input =new Stack<Integer>();
input.push(4);
input.push(1);
input.push(6);
input.push(0);
Stack<Integer> res =sort(input);
for(int e: res)
System.out.print(e);
System.out.println();
while(!res.isEmpty())
System.out.print(+res.pop());
}
static Stack sort(Stack<Integer> input){
Stack<Integer> temp = new Stack<Integer>(); //Make temp the sorted satck
int key;
while(!input.isEmpty()){
key=input.pop();
// while temporary stack is not empty and
// rear of temp is greater than key
while(!temp.isEmpty() && key<temp.peek()){
// pop from temporary stack and
// push it to the input stack
input.push(temp.pop());
}
// push temp in tempory of stack
temp.push(key);
}
return temp;
}
}