9.1-2 Find the second smallest of n elements.
来源:互联网 发布:阿里云 mysql 编辑:程序博客网 时间:2024/06/05 21:09
9.1-1
comparisons in the worst case. (Hint: Also find the smallest element.)
I thinked about it for some time and can't work out the answer. First I want to borrow the idea from the text, kepp track of both the smallest and second smallest element in the array. But found that in worst case that will degrade to O(2n). I decided to look for an answer from the Internet.
Internet Solution:
1. get the smallest between each adjacent pairs and get n/2 elements.
2. again get the smallest from the n/2 elements using the same method.
3. repeat the method iteratively until we have the smallest element.
4. in the process we track the elements that has been compared the the smallest, and the second smallest must in them.
Get the smallest from these elements, that's the second smallest.
Analysis:
1. This is the method that's used in a match. Get the champion using the groups of two method, and get the second from the ones who's defeated by the champion.
2. Exchange space for time. Remember the intermediate information, and use(cache) that for the final or other intermediate purpose.
- 9.1-2 Find the second smallest of n elements.
- Give an O(lg n)-time algorithm to find the median of all 2n elements in arrays X and Y.
- Find the smallest window of a certain sequence
- find the th smallest
- Find the two non-repeating elements in an array of repeating elements
- The Elements of Style
- The Elements of Style
- The Elements of Style
- give two sorted array, find the k-th smallest element of union of A and B
- P26 (**) Generate the combinations of K distinct objects chosen from the N elements of a list.
- find the smallest subtree that contains all of the tree's deepest nodes
- The second kind of hopeness...
- [LinkedIn] Array of size n and an int k, find all elements that appear/occur more than n/k times
- The Elements of C++ Style
- The Elements of Programming Style
- The Elements of Programming Style
- The Elements of Programming Style
- The Elements of Programming Style
- 将 src中的资源文件自动生成到/web-inf/classes/目录下
- 一个硬中断的完整处理过程(2.4.24版本)
- poj2773 Happy 2006 二分+容斥
- ContaCamw编译手记
- android 来电大头贴显示流程
- 9.1-2 Find the second smallest of n elements.
- HDU--杭电--3415--Max Sum of Max-K-sub-sequence--暴力或单调队列
- DrawText函数
- 关于ListView中notifyDataSetChanged()刷新数据不更新原因
- 为OpenStack制作CentOS镜像的一种方法
- WEB测试要点总结
- Linux exec函数族
- 基于 equals() 的 键值同步锁: generic key managed concurrent lock map
- mysql 触发器