编程之美2.5:寻找最大的K个数

引申:寻找第k大的数:

方法一:

// 选择第k大的数(通过改进快速排序来实现)
public static void SelectShort(int[] array, int low, int high, int k, out int value)
{
int i = low;
int j = high;
int tempItem = array[low];
value = int.MinValue; while (low < high)
{
while (array[high] > tempItem
&& high > low)
{
high--;
} if (low < high)
{
array[low] = array[high];
low++; while (array[low] <= tempItem
&& low < high)
{
low++;
} array[high] = array[low];
high--;
}
} array[low] = tempItem; if (low == array.Length - k)
{
value = array[low];
return;
}
else if (low < array.Length -k) // 第k个元素在右分支中
{
if (low + < j)
{
SelectShort(array, low + , j, k, out value);
}
}
else // (low > k-1)第k个元素在左分支中
{
if (i < low - )
{
SelectShort(array, i, low - ,k,out value);
}
}
}

方法二:

借助编程之美的思想,求前k个最大的数,其中最小的就是。

  // 创建最大堆
public static void CreateStack(int[] array, int startIndex, int lastIndex)
{
for (int root = lastIndex / ; root >= startIndex; root--)
{
int currentRoot = root;
int leftLeafIndex = * root + ; // 左孩子
int rightLeafIndex = leftLeafIndex +; // 右孩子 while (leftLeafIndex <= lastIndex)
{
if (leftLeafIndex < lastIndex
&& rightLeafIndex <= lastIndex
&& array[leftLeafIndex] < array[rightLeafIndex])// 右孩子存在而且右孩子大于左孩子
{
leftLeafIndex++;
} if (array[leftLeafIndex] > array[root])
{
int temp = array[root];
array[root] = array[leftLeafIndex];
array[leftLeafIndex] = temp; currentRoot = * currentRoot + ; //继续寻找是否到了叶子结点
leftLeafIndex = * currentRoot + ;
}
else
{
break;
}
}
}
} //堆排序
public static void StackSort(int[] arrary)
{
CreateStack(arrary, , arrary.Length - ); int temp; for (int j = arrary.Length - ; j > ; j--)
{
temp = arrary[j];
arrary[j] = arrary[];
arrary[] = temp; CreateStack(arrary, , j - );
}
}

编程之美2.5:寻找最大的K个数的更多相关文章

  1. 2017“编程之美”终章:AI之战勇者为王

    编者按:8月15日,第六届微软“编程之美”挑战赛在选手的火热比拼中圆满落下帷幕.“编程之美”挑战赛是由微软主办,面向高校学生开展的大型编程比赛.自2012年起,微软每年都在革新比赛命题.紧跟时代潮流, ...

  2. 编程之美2014挑战赛 复赛 Codehunt平台试题答案

    var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...

  3. LeetCode:Climbing Stairs(编程之美2.9-斐波那契数列)

    题目链接 You are climbing a stair case. It takes n steps to reach to the top. Each time you can either c ...

  4. 24点C++程序实现 编程之美1.16

    解法1,对于任意输入的四个数字,给出一个24点的解法,若无解,则没有输出. 原理参照下图(编程之美原书) 代码如下,仅供参考 // 1.16.cpp : Defines the entry point ...

  5. Python编程之美:最佳实践指南PDF高清完整版免费下载|百度云盘|Python新手到进阶

    百度云盘:Python编程之美:最佳实践指南PDF高清完整版免费下载 提取码:1py6 内容简介 <Python编程之美:最佳实践指南>是Python用户的一本百科式学习指南,由Pytho ...

  6. 寻找最大的k个数问题

    这是编程之美书第2.5节的一道题目. 各种解法: 解法一,用nlgn复杂度的排序算法对数组进行从大到小排序,取前K个.但这方法做了两件不必要做的事:它对想得到的K个数进行了排序,对不想得到的n-K个数 ...

  7. 算法系列:寻找最大的 K 个数

    Copyright © 1900-2016, NORYES, All Rights Reserved. http://www.cnblogs.com/noryes/ 欢迎转载,请保留此版权声明. -- ...

  8. 算法练习:寻找最小的k个数

    参考July的文章:http://blog.csdn.net/v_JULY_v/article/details/6370650 寻找最小的k个数题目描述:查找最小的k个元素题目:输入n个整数,输出其中 ...

  9. 第2章 数字之魅——寻找最大的K个数

    寻找最大的K个数 问题描述 在面试中,有下面的问答: 问:有很多个无序的数,我们姑且假定它们各不相等,怎么选出其中最大的若干个数呢? 答:可以这样写:int array[100] …… 问:好,如果有 ...

随机推荐

  1. express+nodecoffee写passport登录验证实例(二)

    二:实现登录认证 passport官网文档:  http://passportjs.org/guide/ passport验证使用一种被称为“策略”的方式来验证请求,策略支持3种类型的验证:用户名密码 ...

  2. 《Play for Java》学习笔记(二)基本的CRUD应用

    注解:  CRUD——Create,Retrieve, Update, Delete 文件结构

  3. js 日期时间控制器

    /////////////////////////调用实例 // <div> // <span>交易查询:</span> <span>从 // < ...

  4. 一些qml资料

    qml开发ios应用 http://www.seanyxie.com/qt-qml%E7%A7%BB%E5%8A%A8%E5%BC%80%E5%8F%91%E4%B9%8B%E5%9C%A8ios%E ...

  5. Socket通信(转)

    一.Socket通信简介 Android与服务器的通信方式主要有两种,一是Http通信,一是Socket通信.两者的最大差异在于,http连接使用的是“请求—响应方式”,即在请求时建立连接通道,当客户 ...

  6. NSDateFormatter 根据时间戳求出时间

    NSDateFormatter 根据时间戳求出时间 - (void)detailWithStyle:(NSString*)style time:(NSInteger)time { // NSStrin ...

  7. bzoj 2127: happiness

    #include<cstdio> #include<iostream> #include<cstring> #define M 100009 #define inf ...

  8. subString用法,字符串保持一定位数,不足补0

    Substrinig(a,b): 从下标a开始截取,共截取b位 实现:一串数字,中间两位数字+2,生成新的一串数字 "; , number.Length - );//前8位 );//后6位 ...

  9. WEKA使用教程(界面工具的用法)

    WEKA使用教程 目录 1. 简介2. 数据格式3.数据准备4. 关联规则(购物篮分析)5. 分类与回归6. 聚类分析 1. 简介 WEKA的全名是怀卡托智能分析环境(Waikato Environm ...

  10. Java并发编程(一) 两种实现多线程的方法(Thread,Runnable)

    Java中实现多线程的方法有两种: 继承Thread类和实现Runnable方法,并重写Run方法,然后调用start()方法启动线程.使用Runnable会比Thread要好很多,主要是以下三个原因 ...