查找一个数组中最小的前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 ...
随机推荐
- 修复ubunut桌面
title: 修复ubunut桌面 tags: 桌面, ubuntu grammar_cjkRuby: true --- ,按下Ctrl+Alt+F2.这会让你进入一个命令行界面而不是默认的用户桌面界 ...
- [NEUQ-OJ] 1012 SZ斐波拉契数列
一道水题,让我看清基础我的基础是多么薄弱. 递归,数组清零,数组名/变量名重复层出不穷...路漫漫啊.......... http://ncc.neuq.edu.cn/oj/problem.php?i ...
- [SOJ] Ordering Tasks
1940. Ordering Tasks Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description John has n task ...
- 洛谷-乘积最大-NOIP2000提高组复赛
题目描述 Description 今年是国际数学联盟确定的“2000――世界数学年”,又恰逢我国著名数学家华罗庚先生诞辰90周年.在华罗庚先生的家乡江苏金坛,组织了一场别开生面的数学智力竞赛的活动,你 ...
- sql第一天
关系数据库中的关系指的就是表 table 表 Column 列 Field 字段 Row 行 非空约束 not null 主键约束(PK)primary key constraint 唯 ...
- C# DataTable转实体通用方法
public static class DataTableHelper { public static T GetEntity<T>(DataTable table) where T : ...
- 图片验证码自动识别,使用tess4j进行验证码自动识别(java实现)
1.下载tess4j依赖的jar包,maven中央库地址:<dependency> <groupId>net.sourceforge.tess4j< ...
- selenium中的webdriver定位元素失败的常见原因
自动化测试中经常会出现无法定位元素的情况,报selenium.common.exceptions.NoSuchElementException错误 Frame/Iframe原因定位不到元素: 这个是最 ...
- L2-010. 排座位
L2-010. 排座位 题目链接:https://www.patest.cn/contests/gplt/L2-010 并查集 相关题目:L2-007. 家庭房产,L3-003. 社交集群 下午打的时 ...
- ios打开系统自带APP
打开系统自带app 打开系统设置: [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root= ...