利用快速排序原理找出数组中前n大的数
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#define MAX_SIZE 400001
// 生成不重复的随机数序列写入文件
void gen_test_data(uint32_t cnt)
{
if( cnt >= MAX_SIZE){printf("cnt too largr\n");return;}
uint32_t i = ;
char buf[MAX_SIZE];
for(;i < cnt;++i){buf[i] = ;}
uint32_t n = ;
char file_name[];
snprintf(file_name,,"test_data_%d.txt",cnt);
FILE *fp = fopen(file_name,"w");
if(NULL == fp){printf("open %s error!\n",file_name);return;}
while(n < cnt)
{
int32_t nRand = rand() % cnt;
while(buf[nRand] == )nRand = (nRand + )%cnt;
buf[nRand] = ;
fprintf(fp,"%d ",nRand);
++n;
}
fclose(fp);
} // 读取文件
void read_data(int32_t arr[],const uint32_t size,uint32_t *cnt,const int32_t data_cnt)
{
FILE *fp = NULL;
char file_name[];
if(data_cnt > size){printf("data_cnt too largr\n");return;}
snprintf(file_name,,"test_data_%d.txt",data_cnt);
fp = fopen(file_name,"r");
if(NULL == fp){printf("open %s error!\n",file_name);return;}
while(!feof(fp) && *cnt < size)
{
fscanf(fp,"%d ",&arr[*cnt]);
(*cnt)++;
}
fclose(fp);
} // 快速排序
void quick_sort(int32_t arr[],int32_t low,int32_t high)
{
if(low >= high)return;
int32_t i = low,j = high,tmp = arr[i];
while(i<j)
{
while(i<j && arr[j] <= tmp)j--;
if(i<j){arr[i] = arr[j];i++;}
while(i<j && arr[i] > tmp)i++;
if(i<j){arr[j] = arr[i];j--;}
}
arr[i] = tmp;
quick_sort(arr,low,i-);
quick_sort(arr,i+,high);
} // 找出最大n个数
void get_topn(int32_t arr[],int32_t low,int32_t high,const int32_t topn)
{
if(low >= high || topn > high)return;
int32_t i = low,j = high,tmp = arr[i];
while(i<j)
{
while(i<j && arr[j] < tmp)j--;
if(i<j)arr[i++] = arr[j];
while(i<j && arr[i] >= tmp)i++;
if(i<j)arr[j--] = arr[i];
}
arr[i] = tmp;
int32_t n = i - low + ;
if (n == topn)return;
else if (n > topn)
get_topn(arr, low, i-, topn);
else if (n < topn)
get_topn(arr, i+, high, topn - n);
} void dump(int32_t arr[],const uint32_t cnt)
{
uint32_t i = ;
for(;i < cnt;++i)
{
printf("%6d ",arr[i]);
}
printf("\n");
} int32_t main(int32_t argc,char *argv[])
{
int32_t data_cnt = ,top = ;
int32_t arr[MAX_SIZE];
uint32_t cnt = ;
gen_test_data(data_cnt);
read_data(arr,MAX_SIZE,&cnt,data_cnt);
get_topn(arr,,cnt-,top);
quick_sort(arr,,top-);
dump(arr,top);
//quick_sort(arr,0,cnt);
//dump(arr,cnt);
return ;
}
利用快速排序原理找出数组中前n大的数的更多相关文章
- python找出数组中第二大的数
#!usr/bin/env python #encoding:utf-8 ''''' __Author__:沂水寒城 功能:找出数组中第2大的数字 ''' def find_Second_large_ ...
- 找出数组前N大的数
这个题也是个比较有名的面试题.当然有很多变种. 题目意思基本是:从一个数据量很大的数组里找前N大的元素.不允许排序. 这个题有两个比较好的思路: 思路一:用快速排序的思想,是思想,不是要排序; 思路二 ...
- 利用堆排序找出数组中前n大的元素
#include <stdio.h> #include <stdint.h> #include <stdlib.h> #include <time.h> ...
- [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 ...
- 剑指offer.找出数组中重复的数字
题目: 给定一个长度为 n 的整数数组 nums,数组中所有的数字都在 0∼n−1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次.请找出数组中任意一个重复的数 ...
- 【Offer】[3-1] 【找出数组中重复的数字】
题目描述 思路 Java代码 代码链接 题目描述 在一个长度为n的数组里的所有数字都在0~n-1的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次. 请找出数组中任 ...
- 前端算法题:找出数组中第k大的数字出现多少次
题目:给定一个一维数组,如[1,2,4,4,3,5],找出数组中第k大的数字出现多少次. 例如:第2大的数是4,出现2次,最后输出 4,2 function getNum(arr, k){ // 数组 ...
- 一起来刷《剑指Offer》-- 题目一:找出数组中重复的数字(Python多种方法实现)
数组中重复的数字 最近在复习算法和数据结构(基于Python实现),然后看了Python的各种"序列"--比如列表List.元组Tuple和字符串String,后期会写一篇博客介绍 ...
- 剑指Offer:找出数组中出现次数超过一半的元素
题目:找出数组中出现次数超过一半的元素 解法:每次删除数组中两个不同的元素,删除后,要查找的那个元素的个数仍然超过删除后的元素总数的一半 #include <stdio.h> int ha ...
随机推荐
- 17111 Football team
时间限制:1000MS 内存限制:65535K 提交次数:0 通过次数:0 题型: 编程题 语言: C++;C Description As every one known, a footbal ...
- 一个工程两个target
有很多的应用有两个版本,可能只是ui上有一些不同,维护两份代码是很麻烦的,这时候我们可以在已有的工程target上copy这个target来达到一份代码两个应用版本的需求 duplicate就可以co ...
- POJ1061 青蛙的约会(线性同余方程)
线性同余方程$ ax \equiv b \pmod n$可以用扩展欧几里得算法求解. 这一题假设青蛙们跳t次后相遇,则可列方程: $$ Mt+X \equiv Nt+Y \pmod L$$ $$ (M ...
- CodeForces Round 195 Div2
A. Vasily the Bear and Triangletime limit per test1 secondmemory limit per test256 megabytesinputsta ...
- css3 flex流动自适应响应式布局样式类
1.再说css3 flex 一旦一个容器赋予了display:flex属性,将会有以下特点: 项目无法设置浮动. 列表的样式会被清除. 无法使用vertical-align设置垂直对齐方式. 目前互联 ...
- jquery面试题里 缓存问题如何解决?
jquery面试题里 缓存问题如何解决? 如果直接用jQuery里的$.ajax()方法的话,去除缓存很简单,只需要配置一下缓存属性cache为false,但如果想要简单写法getJSON(),去除缓 ...
- 你能不用计算机来计算S=a+(a+1)+(a+2) + ...... + b的解的数目吗?
S=a + (a + 1) + (a + 2) + ...... + b(其中a, b > 0) 现在我们要求,给定一个正整数S,求有多少种不同的<a,b>,使得上述的等式成立. 这 ...
- for循环下九九乘法表
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- TCP和UDP的135、137、138、139、445端口的作用
如果全是2000以上的系统,可以关闭137.138.139,只保留445 如果有xp系统,可能以上四个端口全部要打开 无论你的服务器中安装的是Windows 2000 Server,还是Windows ...
- GridVeiw 使用
1. 因使用的是 Mongodb,因此要在 ActiveDataProvider 中指定 key 属性 2. 自定义表格中的按钮 'class' => 'yii\grid\ActionColum ...