包含了Partition函数的多种用法

以及大顶堆操作

 /*************************************************************************
> File Name: 28_KLeastNumbers.cpp
> Author: Juntaran
> Mail: JuntaranMail@gmail.com
> Created Time: 2016年08月31日 星期三 19时45分41秒
************************************************************************/ #include <stdio.h>
#include <bits/stdc++.h> using namespace std; // 大顶堆求最小K个数
typedef multiset<int, greater<int> > intSet;
typedef multiset<int, greater<int> >::iterator setIterator; void GetKLeastNumbers2(int* data, intSet& leastNumbers, int length, int k)
{
leastNumbers.clear(); if (k< || length<k)
return; for (int i = ; i < length; ++i)
{
if (leastNumbers.size() < k)
leastNumbers.insert(data[i]); else
{
setIterator Greatest = leastNumbers.begin();
if (data[i] < *(leastNumbers.begin()))
{
leastNumbers.erase(Greatest);
leastNumbers.insert(data[i]);
}
}
} for (setIterator iter = leastNumbers.begin(); iter != leastNumbers.end(); ++iter)
{
printf("%d ", *iter);
}
printf("\n");
} void swap(int* p, int* q)
{
int temp = *p;
*p = *q;
*q = temp;
} // Partition函数应用
int Partition(int* data, int length, int start, int end)
{
if (data==NULL || length<= || start< || end>=length)
return -; // 令数组第一个数字为标杆
int index = start; // 标杆与数组最后一个元素交换
swap(&data[index], &data[end]); int small = start - ;
for (index = start; index < end; ++index)
{
if (data[index] < data[end])
{
++ small;
if (small != index)
{
swap(&data[index], &data[small]);
}
}
}
++ small;
swap(&data[small], &data[end]); return small;
} // 利用Partiton实现快排
void quickSort(int* data, int length, int start, int end)
{
if (start==end || data==NULL || length<=)
return; int index = Partition(data, length, start, end);
// printf("index is %d\n", index);
if (index > start)
quickSort(data, length, start, index-);
if (index < end)
quickSort(data, length, index+, end);
} // 利用Partition寻找出现次数超过一半的数 (中位数)
int GetMoreThanHalf(int* input, int length)
{
if (input==NULL || length<=)
return -;
int start = ;
int end = length - ;
int index = Partition(input, length, start, end);
int middle = length >> ;
while (index != middle)
{
if (index > middle)
{
end = index - ;
index = Partition(input, length, start, end);
}
else
{
start = index + ;
index = Partition(input, length, start, end);
}
}
int ret = input[middle];
// 检验是否正确
int count2 = ;
for (int i = ; i < length; ++i)
{
if (input[i] == ret)
count2 ++;
}
if (count2* > length)
{
printf("middle number is %d\n", input[middle]);
return ret;
}
else
{
printf("Not Find\n");
return -;
}
} // 利用Partition寻找第K小的数
int GetKthNumber(int* input, int length, int k)
{
if (input==NULL || length<= || k<= || k>length)
return -;
int start = ;
int end = length - ;
int index = Partition(input, length, start, end);
while (index != k - )
{
if (index > k-)
{
end = index-;
index = Partition(input, length, start, end);
}
else
{
start = index + ;
index = Partition(input, length, start, end);
}
}
printf("Kth is %d\n", input[index]);
return input[index];
} // 利用Partition寻找最小K个数
void GetKLeastNumbers(int* input, int length, int* output, int k)
{
if (input==NULL || output==NULL || length<= || k<= || k>length)
{
return;
}
int start = ;
int end = length - ;
int index = Partition(input, length, start, end);
while (index != k - )
{
if (index > k-)
{
end = index - ;
index = Partition(input, length, start, end);
}
else
{
start = index + ;
index = Partition(input, length, start, end);
}
// printf("index is %d\n", index);
}
for (int i = ; i < k; ++i)
output[i] = input[i]; for (int i = ; i < k; ++i)
printf("%d ", output[i]);
printf("\n");
} int main()
{
int k = ;
int nums[] = {,,,,,,,,,};
int length = ;
int output[k] = {}; // 快速排序
quickSort(nums, length, , length-);
for (int i = ; i < length; ++i)
printf("%d ", nums[i]);
printf("\n"); // 求最小K个数
GetKLeastNumbers(nums, length, output, k); // 求第K大的数
GetKthNumber(nums, length, k); // 求数组中超过一半的数(中位数)
GetMoreThanHalf(nums, length); // 大顶堆求最小K个数
intSet leastNumbers;
GetKLeastNumbers2(nums, leastNumbers, length, k);
}

剑指Offer28 最小的K个数(Partition函数应用+大顶堆)的更多相关文章

  1. 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)

    注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...

  2. 剑指 Offer——最小的 K 个数

    1. 题目 2. 解答 2.1. 方法一--大顶堆 参考 堆和堆排序 以及 堆的应用,我们将数组的前 K 个位置当作一个大顶堆. 首先建堆,也即对堆中 [0, (K-2)/2] 的节点从上往下进行堆化 ...

  3. python剑指offer最小的K个数

    题目描述: 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. 思路: 使用快排中的partition思想. ①我们设定part ...

  4. 剑指:最小的k个数

    题目描述 输入 n 个整数,找出其中最小的 K 个数.例如输入 4,5,1,6,2,7,3,8 这 8 个数字,则最小的 4 个数字是 1,2,3,4. 解法 解法一 利用快排中的 partition ...

  5. 剑指Offer——最小的K个数

    题目描述: 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4. 分析: 建一个K大小的大根堆,存储最小的k个数字. 先将K个数进堆 ...

  6. 剑指offer--10.最小的K个数

    边界判断,坑了一下 ----------------------------------------------- 时间限制:1秒 空间限制:32768K 热度指数:375643 本题知识点: 数组 ...

  7. 剑指Offer-29.最小的K个数(C++/Java)

    题目: 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. 分析: 最先想到的是将数组升序排列,返回前k个元素.不过排序的话效率 ...

  8. 用js刷剑指offer(最小的K个数)

    题目描述 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. 牛客网链接 js代码 function GetLeastNumbe ...

  9. 2-剑指offer: 最小的K个数

    题目描述 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. 代码: // 这种topN问题比较常见的是使用堆来解决,最小的k个 ...

随机推荐

  1. mongodb的查询方式与sql语句对比

    下面是sql和Mongodb对应的一些语法: SQL Statement Mongo Query Language Statement CREATE TABLE USERS (a Number, b ...

  2. OC三种方法实现定时器

    在软件开发过程中,我们常常需要在某个时间后执行某个方法,或者是按照某个周期一直执行某个方法.在这个时候,我们就需要用到定时器. 在iOS中有很多方法完成定时器的任务,例如 NSTimer.CADisp ...

  3. JavaScript的因为所以

    各位看官,楼主开始说过写几篇博客,这是这个系列的最后一集.吾以为:了解JavaScript的身世之谜,掌握其近乎心想事成的变量系统,了解其解析运行的偷梁换柱之法,熟悉布大师迂回曲折的OOP实现.那你离 ...

  4. winform系统自动登录实现

    转载:http://www.cnblogs.com/wuhuacong/archive/2012/08/21/2648339.html 系统自动登录有时候很必要,在用户自己绝对信任的电脑上,自动登录对 ...

  5. Adobe Photoshop CC 14.0简体中文特别版32位和64位下载

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  6. 【剑指offer】顺时针打印矩阵

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/26053049 剑指offer上的第20题,九度OJ上測试通过. 题目描写叙述: 输入一个矩 ...

  7. SQL with(unlock)与with(readpast) (转)

    所有Select加 With (NoLock)解决阻塞死锁,在查询语句中使用 NOLOCK 和 READPAST 处理一个数据库死锁的异常时候,其中一个建议就是使用 NOLOCK 或者 READPAS ...

  8. VS2010: Microsoft.TeamFoundation.PowerTools.CheckinPolicies.ChangesetComments 未注冊

    VS2010 缺少Team Foundation Server Power Tools 下载地址: http://visualstudiogallery.msdn.microsoft.com/c255 ...

  9. INDY idhttp Post用法

    http://www.cnblogs.com/tk-del/archive/2013/05/10/3071541.html function Post(AURL: string; ASource: T ...

  10. [Ramda] Filter, Reject and Partition

    We'll learn how to get a subset of an array by specifying items to include with filter, or items to ...