-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstackSort.java
73 lines (72 loc) · 1.59 KB
/
stackSort.java
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
/**
* CC150 3.6 Sort a Stack to ascending order with an aditional Stack.
* Date: Feb 28, 2015
* @author Qing Wang
* Tag: Use the property of stack.
* Following Up: If ultimated stacks are allowed, how to sort?
* Hints for following up: Use modified quicksort or mergesort!
*/
import java.util.*;
public class stackSort {
/**
* Original method.
*/
public void sort(Stack<Integer> stack) {
Stack<Integer> newStack = new Stack<Integer>();
if(stack.size() > 1) {
newStack.push(stack.pop());
while(!stack.isEmpty()) {
if(newStack.peek() > stack.peek()) {
newStack.push(stack.pop());
} else {
int tmp = stack.pop();
int count = 0;
while(!newStack.isEmpty() && newStack.peek() < tmp) {
stack.push(newStack.pop());
count++;
}
newStack.push(tmp);
while(count > 0) {
newStack.push(stack.pop());
count--;
}
}
}
while(!newStack.isEmpty()) {
stack.push(newStack.pop());
}
}
}
public Stack<Integer> Sort(Stack<Integer> stack) {
Stack<Integer> newStack = new Stack<Integer>();
while(!stack.isEmpty()) {
int tmp = stack.pop();
while(!newStack.isEmpty() && newStack.peek() > tmp) {
stack.push(newStack.pop());
}
newStack.push(tmp);
}
return newStack;
}
/**
* Test part!
*/
public static void main(String[] args) {
stackSort ss = new stackSort();
Stack<Integer> s = new Stack<Integer>();
s.push(3);
s.push(7);
s.push(6);
s.push(8);
s.push(1);
s.push(5);
s.push(2);
s.push(9);
s.push(0);
s.push(4);
s = ss.Sort(s);
while(!s.isEmpty()) {
System.out.println(s.pop());
}
}
}