这个题也是个比较有名的面试题.当然有很多变种.

  题目意思基本是:从一个数据量很大的数组里找前N大的元素.不允许排序.

  这个题有两个比较好的思路:

  思路一:用快速排序的思想,是思想,不是要排序;

  思路二:用最大堆的思想.

  

  我暂时只实现了思路一,思路二我之后实现了会补上.

  

  思路一比较简单了.我们先用快排的思想找出第n大的数,然后带上后面n-1个就完事了.因为后面的都比支点数大.

  怎么找第n大的数?我在之前的博客写过,请移步到  找第n大的数  

  

代码:

#include<stdio.h>
#include<stdlib.h> /*找出第n大的数的下标*/
int choose_nth(int a[], int startIndex, int endIndex, int n); /*找出前n大的数*/
void choose_max_n(int a[],int startIndex, int endIndex, int n); int main(int argc, char *argv)
{
int a[] = {,,,,,,,,,,};
int n,i;
printf("数组是:\n");
int an = sizeof(a)/sizeof(int);
for(i = ; i < an; ++i)
printf("%d ",a[i]);
printf("\n"); printf("你想找最大的前面几个数:");
scanf("%d",&n); choose_max_n(a, , an - , n); return ;
} void choose_max_n(int a[], int startIndex, int endIndex, int n)
{
int i = choose_nth(a, startIndex, endIndex, n); printf("最大的前N个数是:\n");
for(; i <= endIndex; ++i)
printf("%d ",a[i]);
printf("\n");
} int choose_nth(int a[], int startIndex, int endIndex, int n)
{
int midOne = a[startIndex];
int i = startIndex, j = endIndex;
if(i == j)
return i; if(i < j)
{
while(i < j)
{
for(; i < j; j--)
if(a[j] < midOne)
{
a[i++] = a[j];
break;
} for(; i < j; i++)
if(a[i] > midOne)
{
a[j--] = a[i];
break;
} } a[i] = midOne;
int th = endIndex - i + ; if(th == n)
{
return i;
}
else
{
if(th > n)
{
return choose_nth(a, i + , endIndex, n);
}
else
{
return choose_nth(a, startIndex, i - , n - th);
}
} }
}

结果:

数组是:

你想找最大的前面几个数:
最大的前N个数是:

之后会补上用最大堆思想来做的代码.

找出数组前N大的数的更多相关文章

  1. python找出数组中第二大的数

    #!usr/bin/env python #encoding:utf-8 ''''' __Author__:沂水寒城 功能:找出数组中第2大的数字 ''' def find_Second_large_ ...

  2. 利用快速排序原理找出数组中前n大的数

    #include <stdio.h> #include <stdint.h> #include <stdlib.h> #define MAX_SIZE 400001 ...

  3. 利用堆排序找出数组中前n大的元素

    #include <stdio.h> #include <stdint.h> #include <stdlib.h> #include <time.h> ...

  4. [LeetCode] Find All Numbers Disappeared in an Array 找出数组中所有消失的数字

    Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and ot ...

  5. 输出前 k 大的数

    总时间限制: 10000ms 单个测试点时间限制: 1000ms 内存限制: 65536kB 描述 给定一个数组,统计前k大的数并且把这k个数从大到小输出. 输入 第一行包含一个整数n,表示数组的大小 ...

  6. 前端算法题:找出数组中第k大的数字出现多少次

    题目:给定一个一维数组,如[1,2,4,4,3,5],找出数组中第k大的数字出现多少次. 例如:第2大的数是4,出现2次,最后输出 4,2 function getNum(arr, k){ // 数组 ...

  7. 找出数组中出现次数超过一半的数,现在有一个数组,已知一个数出现的次数超过了一半,请用O(n)的复杂度的算法找出这个数

    找出数组中出现次数超过一半的数,现在有一个数组,已知一个数出现的次数超过了一半,请用O(n)的复杂度的算法找出这个数 #include<iostream>using namespace s ...

  8. 剑指offer:1.找出数组中重复的数(java版)

    数组中重复的数:题目:找出数组中重复的数,题目描述:在一个长度为n的数组里的所有数字都在0到n-1的范围内.数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任 ...

  9. hdu1280 前m大的数(数组下标排序)

    前m大的数 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

随机推荐

  1. Linux(CentOS)常用命令

    http://fedoranews.org/alex/tutorial/rpm/3.shtml rpm.org rpm -qa|grep mysql 查询已安装的含有mysql的包. mv 移动文件. ...

  2. SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-004-使用AspectJ’s pointcut expression language定义Pointcut

    一. 1.在Spring中,pointcut是通过AspectJ’s pointcut expression language来定义的,但spring只支持它的一部分,如果超出范围就会报Illegal ...

  3. Emberjs View and Route

    index.html <!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset=& ...

  4. c while 循环

    c代码: #include <stdio.h> int main(void){ unsigned long sum=1UL; unsigned int j=1U; unsigned ; p ...

  5. 常用linux命令合集(持续更新中)

    我的博客:www.while0.com 开发调试 readelf-a 查看elf文件中的内容 hexdump -C 用16进制查看文件 objdump -d 反汇编目标文件 nm 查看目标文件或者可执 ...

  6. 绕过kernel模块版本校验检测

    kernel module version check bypass . 举例说明 . 内核是怎么实现的 . 怎样去突破 . 总结 . 举例说明 Linux内核版本很多,升级很快,2个小内核版本中内核 ...

  7. bzoj1056

    花了一上午大概复习了一下splay,treap 像这种裸的数据结构题在js应该会越来越少 不过练练手也好, 这就是平衡树+hash,如果这是单纯的BST应用,还是写treap吧,好调试 ;       ...

  8. C# MVC模式下商品抽奖

    很久没有写博客,于是就把最近项目需求的一个抽奖功能给整理了下,语言表达能力不好,写的不好请勿吐槽,一笑而过就好.好了下面开始说说这个抽奖功能.因为涉及到公司的项目所以一些敏感的地方均已中文代替. 首先 ...

  9. C# 根据Word模版生成Word文件

    指定的word模版 2,生成word类 添加com Microsoft word 11.0 Object Library 引用 using System; using System.Collectio ...

  10. Yet Another Scheme Introduction学习

    Chapter 2 Function exact->inexact is to convert from fractional numbers to floating point numbers ...