顾名思义(?)类似于单调栈?维护一个单调递减的栈。一旦准备入栈的元素大于栈顶元素,栈一直弹出直到准备入栈的元素小于等于栈顶元素,弹出的元素压入另一个tmp栈中。

 #include <iostream>
#include <stack>
#include <cstdlib>
#include <ctime>
using namespace std; void StackSort(stack<int>& s)
{
stack<int> tmp;
while(!s.empty()){
tmp.push(s.top());
s.pop();
}
while(!tmp.empty()){
if(s.empty()){s.push(tmp.top());tmp.pop();}
else{
int x=tmp.top();
tmp.pop();
if(x<=s.top()) s.push(x);
else{
while(!s.empty()&&x>s.top()){
tmp.push(s.top());
s.pop();
}
s.push(x);
}
}
}
} void display(stack<int> s)
{ while (!s.empty()) cout<<s.top()<<endl,s.pop();
cout<<endl;
}
int main(int argc,char**argv)
{ const int n{};
srand((unsigned)time());
stack<int> s;
for (int i{};i<n;i++) s.push(rand()%);
cout<<"Before sorting:"<<endl<<endl; display(s);
cout<<"After sorting:"<<endl; StackSort(s); display(s);
return ;
}

s:

tmp:  8 7 9

s:   9

tmp:  8 7

s:   9 7

tmp: 8

s:  9 8

tmp: 7

s:  9 8 7

tmp:

最新文章

  1. Poj The xor-longest Path 经典题 Trie求n个数中任意两个异或最大值
  2. 实用控件分享:自定义逼真相机光圈View
  3. [FPGA] 2、新建并运行一个工程
  4. SQLServer中的死锁的介绍
  5. 在页面头部&lt;!DOCTYPE html ....&gt; 前面不能有任何输出
  6. js onclick=&quot;return test()&quot;事件返回值,对有些事件,会影响默认动作的执行。如:onclick和onsubmit
  7. 【KMP+DP】Count the string
  8. 事件绑定之.bind()
  9. 质因数分解的rho以及miller-rabin
  10. SpringMVC(五):@RequestMapping下使用@RequestParam绑定请求参数值
  11. 快速沃尔什变换(FWT)及K进制异或卷积&amp;快速子集变换(FST)讲解
  12. yii2 gridview默认排序
  13. Spring-Boot配置文件web性能(服务器)配置项
  14. 关闭linux服务器防火墙
  15. 关于Bootstrap的入门知识
  16. jquery原理的简单分析,让你扒开jquery的小外套。
  17. [leetcode]Convert Sorted List to Binary Search Tree @ Python
  18. Xamarin.Android RelativeLayout
  19. 使用抓包工具pproxy
  20. hdu 1114 dp动规 Piggy-Bank

热门文章

  1. html table内容不随标题滚动
  2. UpdateData
  3. .net core发布程序
  4. jquery 对 table 的操作
  5. php第十四节课
  6. 洛谷——P1196 [NOI2002]银河英雄传说
  7. Luogu P1041 [2003NOIP提高组]传染病控制
  8. BZOJ 1617 Usaco 2008 Mar. River Crossing渡河问题
  9. [国家集训队2012]JZPFAR
  10. UE 高亮 一个或多个关键字的方法