nth_element 测试程序
/********************************************************************
created: 2014/04/29 11:35
filename: nth_element.cpp
author: Justme0 (http://blog.csdn.net/justme0) purpose: nth_element
*********************************************************************/ #include <cstdio>
#include <cstdlib>
#include <cstring> typedef int Type; template <class T>
inline T * copy_backward(const T *first, const T *last, T *result) {
const ptrdiff_t num = last - first;
memmove(result - num, first, sizeof(T) * num);
return result - num;
} /*
** 将 value 插到 last 前面(不包括 last)的区间
** 此函数保证不会越界(主调函数已判断),因此以 unguarded_ 开头
*/
template <class RandomAccessIterator, class T>
void unguarded_linear_insert(RandomAccessIterator last, T value) {
RandomAccessIterator next = last;
--next;
while(value < *next) {
*last = *next;
last = next;
--next;
}
*last = value;
} /*
** 将 last 处的元素插到[first, last)的有序区间
*/
template <class RandomAccessIterator>
void linear_insert(RandomAccessIterator first, RandomAccessIterator last) {
Type value = *last;
if (value < *first) { // 若尾比头小,就将整个区间一次性向后移动一个位置
copy_backward(first, last, last + );
*first = value;
} else {
unguarded_linear_insert(last, value);
}
} template <class RandomAccessIterator>
void insertion_sort(RandomAccessIterator first, RandomAccessIterator last) {
if (first == last) {
return ;
} for (RandomAccessIterator ite = first + ; ite != last; ++ite) {
linear_insert(first, ite);
}
} template <class T>
inline const T & median(const T &a, const T &b, const T&c) {
if (a < b) {
if (b < c) {
return b;
} else if (a < c) {
return c;
} else {
return a;
}
} else if (a < c) {
return a;
} else if (b < c) {
return c;
} else {
return b;
}
} template <class ForwardIterator1, class ForwardIterator2>
inline void iter_swap(ForwardIterator1 a, ForwardIterator2 b) {
Type tmp = *a; // 源码中的 T 由迭代器的 traits 得来,这里简化了
*a = *b;
*b = tmp;
} /*
** 设返回值为 mid,则[first, mid)中迭代器指向的值小于等于 pivot;
** [mid, last)中迭代器指向的值大于等于 pivot
** 这是 STL 内置的算法,会用于 nth_element, sort 中
** 笔者很困惑为什么不用 partition
*/
template <class RandomAccessIterator, class T>
RandomAccessIterator unguarded_partition(RandomAccessIterator first, RandomAccessIterator last, T pivot) {
while(true) {
while (*first < pivot) {
++first;
}
--last;
while (pivot < *last) { // 若 std::partition 的 pred 是 IsLess(pivot),这里将是小于等于
--last;
}
if (!(first < last)) { // 小于操作只适用于 random access iterator
return first;
}
iter_swap(first, last);
++first;
}
} template <class RandomAccessIterator>
void nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last) {
while (last - first > ) {
RandomAccessIterator cut = unguarded_partition(first, last, Type(median(
*first,
*(first + (last - first) / ),
*(last - ))));
if (cut <= nth) {
first = cut;
} else {
last = cut;
}
}
insertion_sort(first, last);
} int main(int argc, char **argv) {
int arr[] = {, , , , , , , , , , };
int size = sizeof arr / sizeof *arr; nth_element(arr, arr + , arr + size); for (int i = ; i < size; ++i) {
printf("%d ", arr[i]); // 20 12 22 17 17 22 23 30 30 33 40
}
printf("\n"); system("PAUSE");
return ;
}
nth_element 测试程序的更多相关文章
- Spring+SpringMvc+Mybatis框架集成搭建教程三(框架整合测试程序开发)
框架整合测试程序开发 (1).在mysql数据库中创建t_user表,sql语句如下 CREATE TABLE `t_user` ( `id` bigint(20) NOT NULL AUTO_INC ...
- Junit初级编码(一)第一个Junit测试程序
序,Junit测试是单元测试的一个框架,提供了很多方法,供我们快速开展单元测试.目前最新版本JAR包为4.12,官网地址为http://junit.org/ 一.第一个Junit测试程序 1 去官网下 ...
- 【iCore3 双核心板】DEMO 1.0 测试程序发布
iCore3 Demo V1.0 程序说明 一.概要 本资料包包含5个文件夹: 1.“arm”里是 icore3上 arm的程序包,开发环境为 KEIL 5.17: 2.“fpga”里是 icore3 ...
- float数据在内存中是怎么存储的 AND IEEE754测试程序
float类型数字在计算机中用4个字节存储.遵循IEEE-754格式标准: 一个浮点数有2部分组成:底数m和指数e 底数部分 使用二进制数来表示此浮点数的实际值指数部分 占用8bit的二进制数,可表示 ...
- hdu 1031 (partial sort problem, nth_element, stable_partition, lambda expression) 分类: hdoj 2015-06-15 17:47 26人阅读 评论(0) 收藏
partial sort. first use std::nth_element to find pivot, then use std::stable_partition with the pivo ...
- STL 源码分析《2》----nth_element() 使用与源码分析
Select 问题: 在一个无序的数组中 找到第 n 大的元素. 思路 1: 排序,O(NlgN) 思路 2: 利用快排的 RandomizedPartition(), 平均复杂度是 O(N) 思路 ...
- php测试程序运行时间和占用内存情况
php测试程序运行时间和占用内存情况: $HeaderTime = microtime(true);//参数true表示返回浮点数值 /** *CODE */ printf(" total ...
- C# 测试程序运行时间和cpu使用时间
方法一 Stopwatch类测试程序运行时间和cpu使用时间 添加命名空间using System.Diagnostics;使用实例如下 private Stopwatch sw = new Stop ...
- 学习实践:使用模式,原则实现一个C++自动化测试程序
个人编程中比较喜欢重构,重构能够提高自己的代码质量,使代码阅读起来也更清晰.但是重构有一个问题,就是如何保证重构后带代码实现的功能与重构前的一致,如果每次重构完成后,对此不闻不问,则会有极大的风险,如 ...
随机推荐
- 通过js获取cookie的实例及简单分析
今天碰到一个在firefox下swfupload 上传时session不一致问题 在一个项目遇到多文件上传时,firefox下,服务器端的session获取不一致问题. 解决办法: 解决办法:将ses ...
- Mono 异步加载数据更新主线程
主要是用 async和 await 调用 RunOnUiThread来更新. 调用函数: //异步加载数据开始 doInBackground (); //异步加载数据开始end protected a ...
- Oracle去掉字符串首尾
今天刚注册博客,与大家分享一下今天的新的: 今天在报表中碰到这样一个需求,数据库里面的一个字段是其他的3个字段合成的,但是现在读取数据只要中间的那一部分, 思考了许久这个字段的中间部分不是固定的,头和 ...
- HTML 浏览器显示控制
//强制浏览器以最高版本运行页面 <meta http-equiv="X-UA-Compatible" content="IE=edge,Chrome=1" ...
- stimulsoft Report报表使用笔记
1.使用设计器设计mrt报表模板,或者从其他文件复制修改 2.删除business object 数据源 3.使用代码添加数据源 ParcelChangeItem change = new Parce ...
- CA签发工具
#!/bin/bash #author Sun Ying #date:2015-12-17 if [ $# -lt 1 ];then echo -e "\033[34mUsage: `bas ...
- [MAC]OS X Mavericks 10.9.5 / 10.10.2 VMWare vmdk镜像,解压就能用!
用起来是比VirtualBox好使很多: 1.鼠标很灵敏,不像Vbox那么飘 2.显卡有驱动,VM可以配置显示器尺寸,完美支持网络.语音.视频,直接搞全屏,还能看电影 3.USB设备支持热插拔 4.支 ...
- python基础知识8——模块1——自定义模块和第三方开源模块
模块的认识 模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需 ...
- sys模块和os模块,利用sys模块生成进度条
sys模块import sysprint(sys.argv)#sys.exit(0) #退出程序,正常退出exit(0)print(sys.version) #获取 ...
- linux下mv命令使用方法
1.作用mv命令来为文件或目录改名或将文件由一个目录移入另一个目录中.该命令等同于DOS系统下的ren和move命令的组合.它的使用权限是所有用户.2.格式mv [options] 源文件或目录 目标 ...