排序---快速排序
来源:互联网 发布:weblogic内存溢出linux 编辑:程序博客网 时间:2024/06/03 09:27
#include <iostream>#include <stack>using namespace std;//快速排序void myprintf(int *arr,int length){ for(int i=0;i<length;i++) { cout<<arr[i]<<" "; } cout<<endl;}static int Partition(int *arr,int low,int hight){ if(arr == NULL || low<0 || hight < 0) { throw new exception("Invalid Parameters"); } //基准 int tmp = arr[low]; while(low <hight) { //尾部开始找到第一个比基准小的数 while(low<hight && arr[hight]>=tmp) { hight--; } if(low == hight) { break; } else { arr[low] = arr[hight]; } //从头找到第一个比基准大的数 while(low<hight && arr[low]<=tmp) { low++; } if(low == hight) { break; } else { arr[hight] = arr[low]; } } arr[low] = tmp; return low;}void Quick(int *arr,int low,int hight){ if(low == hight) { return; } int index = Partition(arr,low,hight); if(index > low) { Quick(arr,low,index-1); } if(index < hight) { Quick(arr,index+1,hight); }}//用递归来解决问题void QuickSort(int *arr,int length){ Quick(arr,0,length-1);}//用栈来模拟递归的过程void QuickSort2(int *arr,int length){ if(arr == NULL || length<=0) { throw new exception("Invalid Parameters"); } stack<int> vec; int low = 0; int hight = length-1; int index = Partition(arr,low,hight); if(index > low) { vec.push(low); vec.push(index-1); } if(index < hight) { vec.push(index+1); vec.push(hight); } while(!vec.empty()) { hight = vec.top(); vec.pop(); low = vec.top(); vec.pop(); index = Partition(arr,low,hight); if(index > low) { vec.push(low); vec.push(index-1); } if(index < hight) { vec.push(index+1); vec.push(hight); } }}int main(){ int arr[]= {6,3,7,8,2,5,1,9}; myprintf(arr,sizeof(arr)/sizeof(arr[0])); QuickSort2(arr,sizeof(arr)/sizeof(arr[0])); cout<<"-----------------"<<endl; myprintf(arr,sizeof(arr)/sizeof(arr[0]));}
阅读全文
0 0
- 排序之快速排序
- 排序算法--快速排序
- 插入排序,快速排序
- 快速排序 冒泡排序
- 排序算法--快速排序
- 排序之 快速排序
- 排序之快速排序
- 快速排序、冒泡排序
- 排序---快速排序
- 排序4快速排序
- 【排序】快速排序
- 排序算法---快速排序
- 快速排序,选择排序
- 排序算法-快速排序
- 【排序】快速排序
- 【基础排序】快速排序
- 排序之------快速排序
- 排序–快速排序
- 转载1
- logging模块学习笔记
- 内网穿透工具之ngrok
- Android实现二维码扫描库
- 【android逆向笔记】(四)火柴人内购逆向破解
- 排序---快速排序
- JS箭头函数的优势在哪里
- Mysql基础:主键和外键
- ARM笔记(看门狗)
- 动态规划算法解析
- 单源最短路径(c语言)
- CSS3的 transition:属性 延续时间 速率变化 延迟时间
- NYOJ zb的生日
- hdu_1908_map知识补全_银行排队问题_给你这个的标号和优先级_2时输出优先级大的3时输出优先级小的_1有新的人进入