The-ith-Element
Brief: the-ith-element,given a array A with n element , return the i-th element of A. A(n,i)
this problem can be solved using quick-sort idear, every time we choose a pivot ,after rearrangement, we know the pivot is the i-th element of A, so we can fixed this problem as the follow action.
i) choose pivot, rearrangement array, get the pivot index of j;
ii) if j > i , then recursion the 1-th partition , A(1-th , i);
iii)if j < i, then recursion the 2-th partition, A(2-th, i-j);
int element(int *array, int left, int right, int index)
{
if(left >= right)
return array[left];
int pivot = array[left];
int i=0, j = 0; for(i=left+1,j = left+1; j <=right; ++j)
{
if(array[j] < pivot)
swap(array[i++], array[j]);
}
swap(array[left], array[i-1]); //pivot is the (i-left)-th element, and pivot's index is i-1 if(index == i-left)
return array[i-1];
else if(index < i-left)
return element(array, left, i-2, index);
else
return element(array, i,right, index-i+left);
}
The-ith-Element的更多相关文章
- Google C++单元测试框架GoogleTest---GMock的CheatSheet文档
CheatSheet文档中包含了GMock所有常用的东西,看了这个基本上就可以用它了,本文接上篇博文:Google C++单元测试框架GoogleTest---Google Mock简介--概念及基础 ...
- [LeetCode] Best Time to Buy and Sell Stock with Cooldown 买股票的最佳时间含冷冻期
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] Best Time to Buy and Sell Stock III 买股票的最佳时间之三
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] Best Time to Buy and Sell Stock II 买股票的最佳时间之二
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] Best Time to Buy and Sell Stock 买卖股票的最佳时间
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- Best Time to Buy and Sell Stock1,2,3,4
找到最低值和最高值 int maxProfit(vector<int>& prices) { ); ; ]; ;i<prices.size();i++) { profit=m ...
- BUG-FREE-For Dream
一直直到bug-free.不能错任何一点. 思路不清晰:刷两天. 做错了,刷一天. 直到bug-free.高亮,标红. 185,OA(YAMAXUN)--- (1) findFirstDuplicat ...
- [LeetCode] Best Time to Buy and Sell Stock II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 4 Best Time to Buy and Sell Stock III_Leetcode
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
随机推荐
- Perl的DATA文件句柄
有太多次写完一个perl程序,需要另外新建一个文件来测试,每次觉得很繁琐,但又不得不这么做.没想到原来perl已经提供了解决方案,这就是DATA. 使用很简单,见下面这个例子: #!/usr/bin/ ...
- ext3文件系统基础
http://blog.csdn.net/haiross/article/category/1488205/2 block size: 是文件系统最小的单位,Ext2/Ext3/Ext4 的区块大 ...
- 淘宝 印风 UDF
http://blog.csdn.net/zhaiwx1987/article/details/6902623
- 构建高性能服务(三)Java高性能缓冲设计 vs Disruptor vs LinkedBlockingQueue--转载
原文地址:http://maoyidao.iteye.com/blog/1663193 一个仅仅部署在4台服务器上的服务,每秒向Database写入数据超过100万行数据,每分钟产生超过1G的数据.而 ...
- nginx1.4.6+php5.5.11+mysql5.6.17+mecache+opcache
要用到的软件:libiconv-1.13.tar.gz libmcrypt-2.5.8.tar.gz mcrypt-2.6.8.tar.gz mhash-0.9.9.9.tar.gz memcache ...
- easyui datagrid 列拖拽
首先easyui 它有提供了拖拽的功能Draggable,那我们就可以想 拖拽标题头到另外的标题头上面我们就对datagrid的columns重新绑定一次 并刷新datagrid这个功能不就行了? & ...
- [WinForm] VS2010发布、打包安装程序(超全超详细)
1. 在vs2010 选择“新建项目”→“ 其他项目类型”→“ Visual Studio Installer→“安装项目”: 命名为:Setup1 . 这是在VS2010中将有三个文件夹, 1.“应 ...
- cookie与session的区别与联系
cookie是什么? cookie是存在于客户端(浏览器). cookie的使用是由浏览器按照一定的原则在后台自动发送给服务器的.浏览器检查所有存储的cookie,如果某个cookie所声明的作用范 ...
- POJ 1679 The Unique MST (最小生成树)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22668 Accepted: 8038 D ...
- sublime text 3 扩展插件SideBarEnhancements用法教程
SideBarEnhancements本是增强侧边栏的插件,这里将教大家如何用来做sublime text 3浏览器预览插件,并可自定义浏览器预览的快捷键. 第一步:安装此插件,搜索相关教程,本博客有 ...