剑指offer 快速排序
来源:互联网 发布:淘宝店铺权重查询系统 编辑:程序博客网 时间:2024/04/19 17:13
1.快速排序,0,1,2..n-1, 选出枢纽元index,交换a[index]与a[n-1],将小于a[n-1]的元素放在左边,大于的元素放在右边,递归分别对两组进行排序
#include<iostream>using namespace std;#include<time.h>int RandomInRange(int left,int right){if(right>left){srand(time(NULL));return left+rand()%(right-left);//产生start~end之间的随机数}}int partion(int *a,int left,int right){if(a==NULL || left<0 || right<left) throw new exception("invalid parameters");int index = RandomInRange(left,right);swap(a[index],a[right]);int i=left-1;for(int j=left;j<right;j++){if(a[j]<a[right]){i++; if(i!=j)swap(a[i],a[j]);}}++i;swap(a[i],a[right]);return i;}void quickSort(int *a,int left,int right){if(left == right)return ;int index= partion(a,left,right);if(index>left)quickSort(a,left,index-1);if(index<right)quickSort(a,index+1,right);}int main(){int a[]={2,8,7,1,3,5,6,4};int length=sizeof(a)/sizeof(int);quickSort(a,0,length-1);for(int i=0;i<length;i++)cout<<a[i]<<" ";}
0 0
- 【剑指offer】快速排序
- 剑指offer--快速排序
- 剑指offer 快速排序
- 剑指offer 快速排序
- 剑指offer 快速排序
- 快速排序-剑指offer
- 剑指offer-快速排序
- 剑指Offer系列---(10)快速排序
- 剑指offer——快速排序
- 【剑指 offer】—— 快速排序
- 剑指Offer——快速排序
- 剑指offer--快速排序变形(5、29)
- 剑指offer—第二章算法之快速排序
- 剑指offer--快速排序递归实现,年龄排序O(N)实现,旋转数组最小值查找
- 剑指offer 归并排序
- 归并排序-剑指offer
- 堆排序-剑指offer
- 剑指offer-排序算法
- 关于Xcode横屏设置问题
- 多线程编程总结
- PyCharm 教程(三)Hello world!
- ubuntu14.04 安装 opencv3.0.0
- IBM.Rational.Rose.Enterprise.v7.0 下载
- 剑指offer 快速排序
- 收集的博客
- 一个平庸程序员的自白
- Codeforces550B:Preparing Olympiad
- 【转】浅谈HTTP中Get与Post的区别
- leetcode--Remove Duplicates from Sorted List II
- PyCharm 教程(四)显示行号
- MapReduce简述、工作流程及新旧API对比
- iCalendar 编程基础:了解和使用 iCal4j