算法导论--->快排
来源:互联网 发布:软件版本说明书 编辑:程序博客网 时间:2024/04/25 15:58
#include <iostream> #include <vector> using namespace std; //交换两个元素值 void swap(int &p,int &q) { int t; t = p; p = q; q = t; } //快速排序算法中的一趟划分函数 int partition(vector<int> &data, int low, int high) { //基准记录 int i = low; int temp = data[low]; for(int j = low + 1; j <= high; ++j ) { if(data[j] <= temp) //查找关键字小于temp的记录,并依次放到data[low]的右侧 swap(data[++i], data[j]); } swap(data[low], data[i]);//将基准记录temp定位,使其左侧记录均不大于temp,右侧均大于temp return i; } //快速排序 void quickSort(vector<int> &data,int low,int high) { int i; if(low<high)//只有一个或无记录时不须排序 { i=partition(data,low,high);//对data[low]到data[high]进行一次划分 quickSort(data,low,i-1);//递归处理左区间 quickSort(data,i+1,high);//递归处理右区间 } } int main() { int n, m; vector<int> input; //输入数据 cout<<"请输入数组的长度: "; cin>>n; cout<<"请对数组赋值: "; for(int i=0; i<n; ++i) { cin>>m; input.push_back(m); } //处理数据 quickSort(input,0,n-1); //输出结果 for(int i=0; i<n; ++i) cout<<input[i]<<" "; cout<<endl; system("pause"); return 0; }
- 算法导论--->快排
- 算法导论之快排
- 快排之算法导论
- 实现《算法导论》里的快排
- 算法导论-------------快排的研究
- 算法导论快排实现(C++)
- 算法导论(一):快速排序与随机化快排
- 算法----快排算法
- 快排算法
- 快排算法
- 快排算法
- 快排算法cmp
- 快排算法
- 排序算法--快排
- 快排算法quickSort
- 快排算法
- 随机快排算法
- [算法]快排
- 各大IT公司技术架构一览
- Android特性与系统架构
- QT 自定义消息响应
- android(MTK)开发中一些问题小结
- 掌握C#自定义泛型类:从初始化说起
- 算法导论--->快排
- python 可视化
- 二分法(递归和非递归)
- sed 用法
- 前言
- 三层交换机:一次路由多次转发
- baidu vs google 1
- 设计模式之中介者模式(Mediator)
- could not be added to the element