快速排序
2015-09-19 21:35
225 查看
用了模板来实现的快速排序,思想是:先选一个随机的值作为关键值,然后让地位和高位的分别和它进行比较,把比它小的都放在它的前面,比它大的都放在后面,然后递归的实现!
#include<iostream> using namespace std; template<typename Type> void print(Type *arr,int length) { for(int i=0;i<=length;++i) { cout<<arr[i]<<" "; } cout<<endl; } template<typename Type> int partition(Type *arr,int low,int high) { Type key=arr[low]; while(low<high) { while(low<high && arr[high]>=key) --high; arr[low]=arr[high]; while(low<high && arr[low]<=key) ++low; arr[high]=arr[low]; } arr[low]=key; return low; } template<typename Type> void Sort(Type *arr,int low,int high) { if(low<high) { int pos=partition(arr,low,high); Sort(arr,low,pos-1); Sort(arr,pos+1,high); } } void main() { double arr[]={1,4,7,5,2,3,8,9}; int length=sizeof(arr)/sizeof(*arr)-1; cout<<"排序前:"<<endl; print(arr,length); Sort(arr,0,length); cout<<"排序后:"<<endl; print(arr,length); }
相关文章推荐
- 黑盒、白盒、灰盒测试的区别
- 2015第38周六
- 爬虫项目总结
- 天天学设计模式3--开放封闭原则
- &nb
- IOS9 最快速适配 五分钟适配ios9
- 我的第一个博客
- Java ServletContext 详解
- 全文检索神器个人修改版
- 如何实现一个无边框Form的移动和改变大小(一)
- Android中BaseActivity的用法
- servletContext
- java 类加载器
- 使用Nginx+Lua搭建AB测试平台
- 快拷神器ExtremeCopy
- Group Anagram
- 如何查看别人的微信公众号的粉丝数
- centos7 安装 redis
- Tomcat详细用法学习(四)
- 文件管理神器TC8.5特别定制版