利用快速排序原理找出数组中前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 ...
随机推荐
- 简单几何(线段相交+最短路) POJ 1556 The Doors
题目传送门 题意:从(0, 5)走到(10, 5),中间有一些门,走的路是直线,问最短的距离 分析:关键是建图,可以保存所有的点,两点连通的条件是线段和中间的线段都不相交,建立有向图,然后用Dijks ...
- SPOJ839 Optimal Marks(最小割)
题目大概说给一张图,每个点都有权,边的权等于其两端点权的异或和,现已知几个点的权,为了使所有边的边权和最小,其他点的权值该是多少. 很有意思的一道题,完全看不出和网络流有什么关系. 考虑每个未知的点$ ...
- 三十分钟掌握STL
这是本小人书.原名是<using stl>,不知道是谁写的.不过我倒觉得很有趣,所以化了两个晚上把它翻译出来.我没有对翻译出来的内容校验过.如果你没法在三十分钟内觉得有所收获,那么赶紧扔了 ...
- 【转载】Linux下makefile详解--跟我一起写 Makefile
概述 —— 什么是makefile?或许很多Winodws的程序员都不知道这个东西,因为那些Windows的IDE都为你做了这个工作,但我觉得要作一个好的和professional的程序员,makef ...
- python 代码片段
print 'dongshen' for word in ['capitalize','these','words']: print word.upper() for i in range(0,5): ...
- NOI2012 : 迷失游乐园
终于补完NOI2012了好开心~ 题目大意:给定一棵树或者环套外向树,求出从中随机选一条简单路径的期望长度,环上点数不超过20. 设 d[x]表示x的度数,ch[x]表示x孩子个数 up[x]表示x向 ...
- linux eclipse cdt make error 127
不知道为啥,copy原来的eclipse环境到新的地方后,编译总是出错: make:*** error 127 解决方案是:属性Properties---C++编译 c++build---build ...
- 《菊与刀》--[美]鲁思·本尼迪克特(Ruth Benedict)
<菊与刀>这本书实在是好看. 下面是一些书摘: * 由在美国曾经全力以赴与之战斗的敌人中,日本人的脾气是最琢磨不透的. * “菊”本是日本皇家家微,“刀”是武家文化的象征. * 日本人的格 ...
- Ubuntu SSH root user cannot login
Open /etc/ssh/sshd_config and check if PermitRootLogin is set to yes. If not, then set it to yes and ...
- hdu 饭卡
本题的思路是:首先如果m<5,直接输出:若m>5,则先拿出5元钱买最贵的东西,这样背包容量就变成了m-5,商品数量为n-1的0/1背包问题. 此题的状态转移方程为:dp[j]=max{dp ...