查找一个数组中最小的前n项
/*****************************************************************
* find the biggest x number in a sequence
* the basic method is the same as the QuickSort
*
* 1. move the smaller one before the pivot
* 2. move the bigger one after the pivot
* 3. determin whether the X matches the pivot's index
* 3.1 if y return the index
* 3.2 if n recursively call the func with proper param
*
*****************************************************************/
#define cnt 10
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int findBiggestX(int* pa, int low, int high, int key);
int main(void)
{
int arr[cnt];
int i=0;
int key = 4;
printf("arr is :\n");
srand((unsigned)time(NULL));//use current time as seed
for(i=0;i<cnt;i++)
{
arr[i]=rand()%100;
printf("%d\t",arr[i]);
}
printf("\n\n");
findBiggestX(arr,0,sizeof(arr)/sizeof(int)-1,key);
for(i=0;i<cnt;i++)
{
printf("%d\t",arr[i]);
}
printf("\n");
for(i=0;i<key;i++)
{
printf("%d\t",arr[i]);
}
printf("\n");
getchar();
return 0;
}
int findBiggestX(int* pa, int low, int high, int key)
{
int pivot = pa[0];
int low_temp = low;
int high_temp = high;
while(low<high)
{
while(pa[high]>=pivot && low<high)
{
high--;
}
pa[low]=pa[high];
while(pa[low]<=pivot && low<high)
{
low++;
}
pa[high]=pa[low];
}
pa[low]=pivot;
if(key-1 == low)
{
return low;
}
else if(key-1 < low)
{
return findBiggestX(pa,low_temp,low,key);
}
else
{
return findBiggestX(pa,low+1,high_temp,key);
}
}
查找一个数组中最小的前n项的更多相关文章
- 【Python实践-5】使用迭代查找一个list中最小和最大值
# -*- coding: utf-8 -*- #使用迭代查找一个list中最小和最大值,并返回一个tuple #遍历list,找到最小值 def findMinAndMax(L): if L==[] ...
- 请使用迭代查找一个list中最小和最大值,并返回一个tuple
如果给定一个list或tuple,我们可以通过for循环来遍历这个list或tuple,这种遍历我们称为迭代(Iteration). 在Python中,迭代是通过for ... in来完成的,而很多语 ...
- python 练习题:使用迭代查找一个list中最小和最大值,并返回一个tuple
# -*- coding: utf-8 -*- # 请使用迭代查找一个list中最小和最大值,并返回一个tuple from collections import Iterable def findM ...
- python 请使用迭代查找一个list中最小和最大值,并返回一个tuple
请使用迭代查找一个list中最小和最大值,并返回一个tuple: 要注意返回的值的类型是不是tuple def findMinAndMax(L): min=0 max=0 if len(L)==0: ...
- 求一个数组中最小的K个数
方法1:先对数组进行排序,然后遍历前K个数,此时时间复杂度为O(nlgn); 方法2:维护一个容量为K的最大堆(<算法导论>第6章),然后从第K+1个元素开始遍历,和堆中的最大元素比较,如 ...
- hash数组快速查找一个字符串中出现最多的字符,并统计出现的次数
如何快速查找一个字符串中出现最多的字符,并统计出现的次数? 可以使用hash数组,也就是关联数组实现快速查找功能. function seek(str) { var hash = []; var ma ...
- [算法]找到无序数组中最小的K个数
题目: 给定一个无序的整型数组arr,找到其中最小的k个数. 方法一: 将数组排序,排序后的数组的前k个数就是最小的k个数. 时间复杂度:O(nlogn) 方法二: 时间复杂度:O(nlogk) 维护 ...
- C#实现如何判断一个数组中是否有重复的元素 返回一个数组升序排列后的位置信息--C#程序举例 求生欲很强的数据库 别跟我谈EF抵抗并发,敢问你到底会不会用EntityFramework
C#实现如何判断一个数组中是否有重复的元素 如何判断一个数组中是否有重复的元素 实现判断数组中是否包含有重复的元素方法 这里用C#代码给出实例 方法一:可以新建一个hashtable利用hasht ...
- 面试题-->写一个函数,返回一个数组中所有元素被第一个元素除的结果
package com.rui.test; import java.util.Random; /** * @author poseidon * @version 1.0 * @date:2015年10 ...
随机推荐
- 获取Java的32位MD5实现
获取Java的32位MD5实现 public static String md5(String s) { char hexDigits[] = {'0','1','2','3','4','5','6' ...
- CodeForces 707D Persistent Bookcase
$dfs$,优化. $return$操作说明该操作完成之后的状态和经过操作$k$之后的状态是一样的.因此我们可以建树,然后从根节点开始$dfs$一次(回溯的时候复原一下状态)就可以算出所有状态的答案. ...
- Linux Tomcat 自启动
使用chkconfig命令 修改tomcat/bin/startup.sh,在开头的地方添加如下内容 #chkconfig: #description:tomcat auto start #proce ...
- php 便利数组方法
数组在PHP中是一个非常强大的武器,用起来方便.容易,由于使用起来异常灵活,用它就可以实现数据结构中的链表.栈.队列.堆以及所谓的字典.集合等,也可以转换成XML格式. 1.使用for for语句遍历 ...
- ****The Toy of Flandre Scarlet
The Toy of Flandre Scarlet Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & ...
- 线性规划?数学?差分约束?Good Bye 2016 C
http://codeforces.com/contest/750/problem/C 反正我不会这道题...为什么那么多人做出来了...我好菜.jpg 题目大意:cf每个人都有分数,每次都会在div ...
- maven 3.3.9-bin 和 maven 3.3.9-src 的区别 以及 maven安装包的 .tar.gz后缀与.zip 后缀的区别
(maven 3.3.9-bin)一个是class的文件包,由java文件编译成的,(maven 3.3.9-src )一个是java文件包即是源码(.tar.gz后缀)是linux的压缩包,(.zi ...
- iframe标签使用总结与注意问题
子页面访问父父页面变量,函数,页面元素 //变量: //在父页面中需定义为全局变量 //子页面中调用 var childFrameVar= parent.ParentVarName; //函数: pa ...
- HTML5入门总结 HTML5API
w3cshools MDN英文 MDN中文 HTML5 HTML5 is the latest evolution of the standard that defines HTML. The t ...
- Android 6.0动态添加权限
Android 6.0加入了动态权限,权限有普通权限和危险权限两种,其中危险权限在6.0以上的手机是需要动态添加权限的,举例:拨打10086//-----------------布局文件------- ...