转:递归算法学习系列之寻找第K大

  1. 概述

国人向来喜欢论资排辈的,每个人都想当老大,实在当不成,当个老二,老三,老K也不错,您一定看过这样的争论: 两个人吵架,一个人非常强势,另外一个忍受不住了便说:"你算老几呀?",下面就通过这篇文章就是要解决找出老几的问题!

2. 应用场景

在向量V[first,last)中查找出第K大元素的值

3. 分析

如果利用排序算法将向量V排好序,那么第K大元素就是索引为v.length-k的元素了,这样能解决问题,但效率不高,因为这相当于为了歼灭敌人一个小队而动用了我们全军的力量,得不偿失,回想快速排序中的分表,每次都将目标向量分为两个子表,左子表中全部小于中间元素v[mid],右边都大于中间元素v[mid],这样就可以减小了查找范围,因为我可以只查找左子表或者右子表就能找到目标元素了。如下图所示,我们可以将向量 v划分成如下

Left(<=KLargest)KLargest Right(>=KLargest)

按照这样的思路,我们仍使用快速排序中的分表策略,首先将向量V从中间位置分开,分成左和右,分好后,中间值的索引如果恰恰等于K,就找到了,否则如果中间元素索引大于K,则在左子表中继续查找,忽略右子表,如果中间值索引小于K,则在右子表中继续查找,如此循环往复。

[@more@]

快速排序中的子表划分函数为:

ExpandedBlockStart.gif ContractedBlock.gif /**/ ///
InBlock.gif/// 交换位置
InBlock.gif///
InBlock.gif///
InBlock.gif///
ExpandedBlockEnd.gif///

None.gif private void Swrap( int [] v, int index1, int index2)
ExpandedBlockStart.gifContractedBlock.gif dot.gif {
InBlock.gifint temp = v[index1];
InBlock.gif v[index1] = v[index2];
InBlock.gif v[index2] = temp;
ExpandedBlockEnd.gif }

ExpandedBlockStart.gifContractedBlock.gif /**/ ///
InBlock.gif/// 将向量V中索引{first,last)划分成两个左子表和右子表
InBlock.gif///
InBlock.gif/// 向量V
InBlock.gif/// 开始位置
ExpandedBlockEnd.gif/// 结束位置

None.gif private int PivotIndex( int [] v, int first, int last)
ExpandedBlockStart.gifContractedBlock.gif dot.gif {
InBlock.gifif (last == first)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifreturn last;
ExpandedSubBlockEnd.gif }

InBlock.gifif (last - first == 1)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifreturn first;
ExpandedSubBlockEnd.gif }

InBlock.gifint mid = (first + last) / 2;
InBlock.gifint midVal = v[mid];
InBlock.gif//交换v[first]和v[mid]
InBlock.gif Swrap(v, first, mid);
InBlock.gifint scanA = first + 1;
InBlock.gifint scanB = last - 1;
InBlock.giffor (; ; )
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gifwhile (scanA <= scanB && v[scanA] < midVal)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif scanA++;
ExpandedSubBlockEnd.gif }

InBlock.gifwhile (scanB > first && midVal <= v[scanB])
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif scanB--;
ExpandedSubBlockEnd.gif }

InBlock.gifif (scanA >= scanB)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifbreak;
ExpandedSubBlockEnd.gif }

InBlock.gif Swrap(v, scanA, scanB);
InBlock.gif scanA++;
InBlock.gif scanB--;
ExpandedSubBlockEnd.gif }

InBlock.gif Swrap(v, first, scanB);
InBlock.gifreturn scanB;
InBlock.gif
ExpandedBlockEnd.gif }

设计一个函数,FindKLargest(int[] v,int first,int last,int k);这个函数包括四个参数:向量V,开始位置first,结束位置last,和第k大中的K,则该函数为:

调用FindKLargest后,因为数组是从小到大排序,所以第K大元素的值为V[v.Length-k];

None.gif void FindKLargest( int [] v, int first, int last, int k)
ExpandedBlockStart.gifContractedBlock.gif dot.gif {
InBlock.gif
InBlock.gif//表示分表中值的索引
InBlock.gif int index = 0;
InBlock.gif index = PivotIndex(v, first, last);
InBlock.gifif (index == k)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif//找到了K大
InBlock.gif return;
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gifif (index > k)
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif//只在左子表中查找
InBlock.gif FindKLargest(v, first, index, k);
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gifelse
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif//只在右子表中查找
InBlock.gif FindKLargest(v, index, last, k);
ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif }


4.运行结果:
原向量 :v = { 100, 200, 50, 23, 300, 560, 789, 456, 123, 258}
first = 0; last = v.Length;k=3
输出:456

5.结论
利用递归算法可以将比较复杂的问题划分为越来越小的小问题,这样能够使复杂问题简单化,这样的思路在系统设计和架构中同样有着至关重要的作用,一个好的架构师,面对复杂的问题,能庖丁解牛般化腐朽为神奇,而坏的却往往适得其反,他们的特长是简单问题复杂化。

6. 项目文件
/Files/jillzhang/FindK.rar

上几篇文章索引:
1.算法:【一列数的规则如下: 1、1、2、3、5、8、13、21、34 ,求第30位数是多少, 用递归算法实现。(C#语言)】
2.大牛生小牛的问题
3.递归算法学习系列一(分而治之策略)
4. 递归算法学习系列二(归并排序)
5.递归算法学习系列之三(快速排序)

http://www.cnblogs.com/jillzhang/archive/2007/10/04/913478.html

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/220284/viewspace-1024315/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/220284/viewspace-1024315/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值