sort numbers with three stacks

s3 是用来存放sort 结果的,整个算法非常像 selection sort/bubble sort

1: 每次从s1中选出最小的一个放在s3 中,其他非最小的放入s2 中

2: 然后把s2 的数字放回到s1 中

循环结束后,s3 中的就是sort 好的结果

 1 public void sort(LinkedList<Integer> s1) {
 2     Deque<Integer> s2 = new LinkedList<>();
 3     Deque<Integer> s3 = new LinkedList<>();
 4     
 5     int size = s1.size(); 
 6     for (int i = 0; i < size; i++) {
 7         int globMin = Integer.MAX_VALUE;
 8         while (!s1.isEmpty()) {
 9             int top = s1.pop();
10             globMin = Math.min(globMin, top);
11             s2.push(top);
12         }
13 
14         s3.push(globMin);
15         while (!s2.isEmpty()) {
16 int top = s2.pop();
17 if (top == globMin) {
18 globMin = Integer.MAX_VALUE;
19 continue;    
20 }
21 s1.push(top);
22         }
23     }
24     
25     while (!s3.isEmpty()) {
26         s1.push(s3.pop());
27     }
28 }
原文地址:https://www.cnblogs.com/davidnyc/p/8726816.html