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

  题目意思基本是:从一个数据量很大的数组里找前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. 1090-Rock, Paper, Scissors

    描述 Rock, Paper, Scissors is a classic hand game for two people. Each participant holds out either a ...

  2. 利用ZABBIX的RPC-JSON作API扩展应用示例

    计划将ZABBIX的一些状态可以在另一个应用的显示GRAPH及链接. 故而在网上找了几个文档,作了一个测试. https://www.zabbix.com/documentation/2.4/manu ...

  3. unity博文搜集

    一.综合篇 1. 脚本 unity3d脚本编程基础 2.Mecanim 使用Mecanim实现连击 3. 数学图形学 U3D需要用到的数学基础  2 4. shader 猫都能学会的Unity3D S ...

  4. 栈和队列的面试题Java

    栈和队列: 面试的时候,栈和队列经常会成对出现来考察.本文包含栈和队列的如下考试内容: (1)栈的创建 (2)队列的创建 (3)两个栈实现一个队列 (4)两个队列实现一个栈 (5)设计含最小函数min ...

  5. html5 spring demo

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

  6. 【POJ】1054 The Troublesome Frog

    题目是非常经典的搜索+剪枝.题意简言之就是,青蛙需要沿着直线踩着踏点通过田地,并且踏点需要至少为3.问哪条路径青蛙踩坏的作物最多.很好的一个条件是青蛙每次移动都是等间距的.题目需要注意将其排序. #i ...

  7. wcf教程

    WCF Tutorial WCF stands for Windows Communication Foundation. It is a framework for building, config ...

  8. bzoj2208

    首先有向图的题目不难想到先tarjan缩点 一个强连通分量中的点的连通数显然是相等: 据说这样直接dfs就可以过了,但显然不够精益求精 万一给定的是一个完全的DAG图怎么办,dfs铁定超时: 首先想, ...

  9. C#中的深拷贝与浅拷贝

    1.基本的概念: 首先我们应该了解一下什么叫深拷贝与浅拷贝(Deep Copy and Shallow Copy). a.浅拷贝(Shallow Copy影子克隆):只复制对象的基本类型,对象类型,仍 ...

  10. VS2010中的调试技巧

    作者: scottgu 这是我的博客中关于VS 2010和.NET 4发布系列的第二十六篇文章. 今天的博文将介绍Visual Studio中的一些实用调试技巧.这是受我朋友Scott Cate (他 ...