-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathsort-a-stack-using-a-temporary-stack.js
98 lines (91 loc) · 1.93 KB
/
sort-a-stack-using-a-temporary-stack.js
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**
* Sort a stack using a temporary stack
*
* Given a stack of integers, sort it in ascending order using another temporary stack.
* Output : [1, 2, 3, 4, 5, 8]
*
* Here is a dry run of above pseudo code.
*
* input: [34, 3, 31, 98, 92, 23]
*
* Element taken out: 23
* input: [34, 3, 31, 98, 92]
* tmpStack: [23]
*
* Element taken out: 92
* input: [34, 3, 31, 98]
* tmpStack: [23, 92]
*
* Element taken out: 98
* input: [34, 3, 31]
* tmpStack: [23, 92, 98]
*
* Element taken out: 31
* input: [34, 3, 98, 92]
* tmpStack: [23, 31]
*
* Element taken out: 92
* input: [34, 3, 98]
* tmpStack: [23, 31, 92]
*
* Element taken out: 98
* input: [34, 3]
* tmpStack: [23, 31, 92, 98]
*
* Element taken out: 3
* input: [34, 98, 92, 31, 23]
* tmpStack: [3]
*
* Element taken out: 23
* input: [34, 98, 92, 31]
* tmpStack: [3, 23]
*
* Element taken out: 31
* input: [34, 98, 92]
* tmpStack: [3, 23, 31]
*
* Element taken out: 92
* input: [34, 98]
* tmpStack: [3, 23, 31, 92]
*
* Element taken out: 98
* input: [34]
* tmpStack: [3, 23, 31, 92, 98]
*
* Element taken out: 34
* input: [98, 92]
* tmpStack: [3, 23, 31, 34]
*
* Element taken out: 92
* input: [98]
* tmpStack: [3, 23, 31, 34, 92]
*
* Element taken out: 98
* input: []
* tmpStack: [3, 23, 31, 34, 92, 98]
*
* final sorted list: [3, 23, 31, 34, 92, 98]
*/
import Stack from 'common/stack';
/**
* @param {Stack} input
* @return {Stack}
*/
const sortStack = input => {
const tmpStack = new Stack();
while (!input.isEmpty()) {
// pop out the first element
const tmp = input.pop();
// while temporary stack is not empty and
// top of stack is greater than temp
while (!tmpStack.isEmpty() && tmpStack.peek() > tmp) {
// pop from temporary stack and
// push it to the input stack
input.push(tmpStack.pop());
}
// push temp in tempory of stack
tmpStack.push(tmp);
}
return tmpStack;
};
export { sortStack };