/*****************************************************************
* 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项的更多相关文章

  1. 【Python实践-5】使用迭代查找一个list中最小和最大值

    # -*- coding: utf-8 -*- #使用迭代查找一个list中最小和最大值,并返回一个tuple #遍历list,找到最小值 def findMinAndMax(L): if L==[] ...

  2. 请使用迭代查找一个list中最小和最大值,并返回一个tuple

    如果给定一个list或tuple,我们可以通过for循环来遍历这个list或tuple,这种遍历我们称为迭代(Iteration). 在Python中,迭代是通过for ... in来完成的,而很多语 ...

  3. python 练习题:使用迭代查找一个list中最小和最大值,并返回一个tuple

    # -*- coding: utf-8 -*- # 请使用迭代查找一个list中最小和最大值,并返回一个tuple from collections import Iterable def findM ...

  4. python 请使用迭代查找一个list中最小和最大值,并返回一个tuple

    请使用迭代查找一个list中最小和最大值,并返回一个tuple: 要注意返回的值的类型是不是tuple def findMinAndMax(L): min=0 max=0 if len(L)==0: ...

  5. 求一个数组中最小的K个数

    方法1:先对数组进行排序,然后遍历前K个数,此时时间复杂度为O(nlgn); 方法2:维护一个容量为K的最大堆(<算法导论>第6章),然后从第K+1个元素开始遍历,和堆中的最大元素比较,如 ...

  6. hash数组快速查找一个字符串中出现最多的字符,并统计出现的次数

    如何快速查找一个字符串中出现最多的字符,并统计出现的次数? 可以使用hash数组,也就是关联数组实现快速查找功能. function seek(str) { var hash = []; var ma ...

  7. [算法]找到无序数组中最小的K个数

    题目: 给定一个无序的整型数组arr,找到其中最小的k个数. 方法一: 将数组排序,排序后的数组的前k个数就是最小的k个数. 时间复杂度:O(nlogn) 方法二: 时间复杂度:O(nlogk) 维护 ...

  8. C#实现如何判断一个数组中是否有重复的元素 返回一个数组升序排列后的位置信息--C#程序举例 求生欲很强的数据库 别跟我谈EF抵抗并发,敢问你到底会不会用EntityFramework

    C#实现如何判断一个数组中是否有重复的元素   如何判断一个数组中是否有重复的元素 实现判断数组中是否包含有重复的元素方法 这里用C#代码给出实例 方法一:可以新建一个hashtable利用hasht ...

  9. 面试题-->写一个函数,返回一个数组中所有元素被第一个元素除的结果

    package com.rui.test; import java.util.Random; /** * @author poseidon * @version 1.0 * @date:2015年10 ...

随机推荐

  1. Mediawiki随笔

    http://www.ibm.com/developerworks/cn/opensource/os-mediawiki/  定制wiki http://www.zzbaike.com/wiki/Me ...

  2. 各种文件的ContentType

    CONTENT_TYPE = { 16 '.load': 'text/html', 17 '.123': 'application/vnd.lotus-1-2-3', 18 '.3ds': 'imag ...

  3. shell获取系统时间

    获取系统时间 date -d"yesterday" +"%F %H:%M:%S" #输出昨天这个时候的时间 date -d"tomorrow" ...

  4. HUD 1171 Big Event in HDU(01背包)

    Big Event in HDU Problem Description Nowadays, we all know that Computer College is the biggest depa ...

  5. linux安装GraphicsMagick

    下载GraphicsMagick-1.3.21.tar.gz 解压:tar -zxvf GraphicsMagick-1.3.21.tar.gz cd /usr/local/GraphicsMagic ...

  6. 常见dos命令

    打开控制面板:win+r  control 服务: win+r  services.msc

  7. 【Python之路】第十一篇--CSS

    css CSS是Cascading Style Sheets的简称,中文称为层叠样式表,用来控制网页数据的表现,可以使网页的表现与数据内容分离. 一.css的四种引入方式 1.行内式 行内式是在标记的 ...

  8. 【HELLO WAKA】WAKA iOS客户端 之一 APP分析篇

    由于后续篇幅比较大,所以调整了内容结构. 全系列 [HELLO WAKA]WAKA iOS客户端 之一 APP分析篇 [HELLO WAKA]WAKA iOS客户端 之二 架构设计与实现篇 [HELL ...

  9. Oracle中 union 和 union all 的区别

    如果我们需要将两个select语句的结果作为一个整体显示出来,我们就需要用到union或者union all关键字. union(或称为联合)的作用是将多个结果合并在一起显示出来. union和uni ...

  10. 《JavaScript高级程序设计》读书笔记 ---理解对象

    上一章曾经介绍过,创建自定义对象的最简单方式就是创建一个Object 的实例,然后再为它添加属性和方法,如下所示.var person = new Object();person.name = &qu ...