包含了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. A*算法为什么是最优的

    图搜索的A*算法有两种情况: hn是可采纳的,但是不是满足一致性 如果满足一致性,A*算法的实现要简单一些:即使不检查closed节点的状态重复,也能得到最优的结果 下面是证明最优性的一些关键点: 1 ...

  2. Nutch 教程

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

  3. xx.exe 中的 0x7c92e4df 处最可能的异常: 0xC0000008: An invalid handle was specified

    今天遇到个超级奇怪的问题,昨天还好端端的程序,今天用VS打开后,在关闭主窗口的时候居然弹出错误提示:xx.exe 中的 0x7c92e4df 处最可能的异常: "0xC0000008: An ...

  4. Codeforces Round #306 (Div. 2) E. Brackets in Implications 构造

    E. Brackets in Implications Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/conte ...

  5. [转载]GMT地形数据总结

    [转载]GMT地形数据总结     原文地址:GMT地形数据总结作者:Jason 转载:http://seisman.info/gmt-topo-grid-datas.html   目前接触到的地形数 ...

  6. DebugView 调试工具

    软件标签: DebugView调试工具 用debugview,打开debugview,运行你的debug版本程序,可以定位到源文件的某一行.在vc源码中需要输出的地方用 OutputDebugStri ...

  7. SmartThreadPool

    https://github.com/amibar/SmartThreadPool 首先是实例化的时候的参数的解释 //Initialize SmartThreadPool & Make lo ...

  8. Using UTL_DBWS to Make a Database 11g Callout to a Document Style Web Service

    In this Document   _afrLoop=100180147230187&id=841183.1&displayIndex=2&_afrWindowMode=0& ...

  9. 使用NIO提升性能

    NIO是New I/O的简称,与旧式的基于流的I/O方法相对,从名字看,它表示新的一套Java I/O标准. 具有以下特性: 传统Java IO,它是阻塞的,低效的.那么Java NIO和传统Java ...

  10. UNIX/Linux网络编程基础:图解TCP/IP协议栈

    目录 1.主机到网络层协议:以太网协议 2.IP协议 3.网际控制报文协议(ICMP) 4.传输控制协议(TCP) 5.用户数据报文协议(UDP) 6.流控制传输协议(SCTP) 7.地址解析协议(A ...