二分partition算法应用
一个二分partition算法,将整个数组分解为小于某个数和大于某个数的两个部分,然后递归进行排序算法。
法一:
int partition(vector<int>&arr, int begin, int end){
int pivot = arr[begin];
// Last position where puts the no_larger element.
int pos = begin;
for(int i=begin+; i!=end; i++){
if(arr[i] <= pivot){
pos++;
if(i!=pos){
swap(arr[pos], arr[i]);
}
}
}
swap(arr[begin], arr[pos]);
return pos;
}
法二:
int partition(vector<int> &nums, int begin, int end)
{
int pivot = nums[begin];
while(begin < end)
{
while(begin < end && nums[--end] >= pivot);
nums[begin] = nums[end];
while(begin < end && nums[++begin] <= pivot);
nums[end] = nums[begin];
}
nums[begin] = pivot;
return begin;
}
经典的快速排序算法,直接上代码:
void quickSort(vector<int> &nums, int begin, int end)
{
if(end - begin <= )
return;
int mid = partition(nums, begin, end); quickSort(nums, begin, mid);
quickSort(nums, mid, end);
}
数组第K大数值查询
class Solution
{
public:
int findKthLargest(vector<int> &nums, int k)
{
int len = nums.size();
int res = ;
int left = ;
int right = len;
while(left < right)
{
int pos = partition(nums, left, right);
if(pos == len-k)
{
res = nums[pos];
break;
}
else if(pos < len-k)
left = pos+;
else
right = pos;
}
return res;
}
int partition(vector<int> &nums, int begin, int end)
{
int pivot = nums[begin];
while(begin < end)
{
while(begin < end && nums[--end] >= pivot);
nums[begin] = nums[end];
while(begin < end && nums[++begin] <= pivot);
nums[end] = nums[begin];
}
nums[begin] = pivot;
return begin;
}
};
二分partition算法应用的更多相关文章
- Partition算法剖析
博文链接:http://haoyuanliu.github.io/2016/12/18/Partition%E7%AE%97%E6%B3%95%E5%89%96%E6%9E%90/ 对,我是来骗访问量 ...
- Partition算法以及其应用详解下(Golang实现)
接前文,除了广泛使用在快速排序中.Partition算法还可以很容易的实现在无序序列中使用O(n)的时间复杂度查找kth(第k大(小)的数). 同样根据二分的思想,每完成一次Partition我们可以 ...
- Partition算法以及其应用详解上(Golang实现)
最近像在看闲书一样在看一本<啊哈!算法> 当时在amazon上面闲逛挑书,看到巨多人推荐这本算法书,说深入浅出简单易懂便买来阅读.实际上作者描述算法的能力的确令人佩服.就当复习常用算法吧. ...
- Java实现的二分查找算法
二分查找又称折半查找,它是一种效率较高的查找方法. 折半查找的算法思想是将数列按有序化(递增或递减)排列,查找过程中采用跳跃式方式查找,即先以有序数列的中点位置为比较对象,如果要找的元素值小 于该中点 ...
- c#-二分查找-算法
折半搜索,也称二分查找算法.二分搜索,是一种在有序数组中查找某一特定元素的搜索算法. A 搜素过程从数组的中间元素开始,如果中间元素正好是要查找的元素,则搜素过程结束: B 如果某一特定元素大于或者小 ...
- 二分查找算法(JAVA)
1.二分查找又称折半查找,它是一种效率较高的查找方法. 2.二分查找要求:(1)必须采用顺序存储结构 (2).必须按关键字大小有序排列 3.原理:将数组分为三部分,依次是中值(所谓的中值就是数组中间位 ...
- 二分查找算法java实现
今天看了一下JDK里面的二分法是实现,觉得有点小问题.二分法的实现有多种今天就给大家分享两种.一种是递归方式的,一种是非递归方式的.先来看看一些基础的东西. 1.算法概念. 二分查找算法也称为折半搜索 ...
- Java学习之二分查找算法
好久没写算法了.只记得递归方法..结果测试下爆栈了. 思路就是取范围的中间点,判断是不是要找的值,是就输出,不是就与范围的两个临界值比较大小,不断更新临界值直到找到为止,给定的集合一定是有序的. 自己 ...
- python函数(4):递归函数及二分查找算法
人理解循环,神理解递归! 一.递归的定义 def story(): s = """ 从前有个山,山里有座庙,庙里老和尚讲故事, 讲的什么呢? ""& ...
随机推荐
- uploadify上传文件(2)--基础语法
隔了好久,因为最近搬家,离开从小生活的城市,来到杭州.找工作.找房子等诸多事宜耽误了这篇文章许久.今天难得闲暇在旅馆中完成uploadify上传文件系列的第二篇--uploadify使用的基础语法. ...
- python2.x和python3.x的区别
一.python2.x和python3.x中raw_input( )和input( )区别 1.在Python2.x中raw_input( )和input( ),两个函数都存在,其中区别为 raw_i ...
- java碎笔
选择表达式 overviewPart1.setMonth_incom(rs.getString("month_incom").equals("")?" ...
- Git & github 使用指南
Git的安装: 1.下载 Git for windows下载网址:https://git-for-windows.github.io/ 2.安装 选择安装路径: 选择组件:默认 是否修改环境变量 : ...
- 简述在javascript和jquery中cookie的使用
html <body onload="cookieJar()"></body> javascript <script href="../st ...
- WIN7下PS/2等键盘失灵无法使用的解决办法
WIN7下PS/2等键盘失灵无法使用的解决办法 装了win7,无意中一天开机,发现键盘不能用了.开始以为键盘坏了,重启看机,一看能进bios,各键正常.然后再重启,进系统,看设备管理器,发现键盘为黄色 ...
- python3.4 UnicodeDecodeError: 'gbk' codec can't decode byte 0xff in position
python3.4 UnicodeDecodeError: 'gbk' codec can't decode byte 0xff in position 实用python的时候 打开一个csv的文件出 ...
- Ubuntu12.04LTS安装好后是空白桌面的解决步骤(更新显卡驱动)
安装完毕启动后,明显慢的要死,登陆后竟然是一个空白的桌面环境,Ctrl+Alt+T 根本没有任何反应.唯一的反应就是右键能够创建文件和文档. 同时打开的窗口没有最大化,最小化及关闭按钮. GOOGLE ...
- AM335x(TQ335x)学习笔记——WM8960声卡驱动移植
经过一段时间的调试,终于调好了TQ335x的声卡驱动.TQ335x采用的Codec是WM8960,本文来总结下WM8960驱动在AM335x平台上的移植方法.Linux声卡驱动架构有OSS和ALSA两 ...
- freemarker报错之十三
1.错误描述 freemarker.core.ParseException: Token manager error: freemarker.core.TokenMgrError: Unknown d ...