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++自动化测试程序
个人编程中比较喜欢重构,重构能够提高自己的代码质量,使代码阅读起来也更清晰.但是重构有一个问题,就是如何保证重构后带代码实现的功能与重构前的一致,如果每次重构完成后,对此不闻不问,则会有极大的风险,如 ...
随机推荐
- View Controller Relationships
Parent-child relationshipsParent-child relationships are formed when using view controller container ...
- winserver2008 R2 64位 企业版 , IIS 配置运行 asp+access 网站
新建网站,程序池由DefaultAppPool 改为 Classic .NET AppPool, 并在 高级设置中,把启用 32位应用程序 设为 true 对 access 所在目录新加 every ...
- 批量Clip
没有建立对应dataset,直接生成featureclass,主要是工作中没几个dataset. # -*- coding: utf-8 -*-__author__ = 'tanshuai' impo ...
- Maven依赖版本冲突的分析及解决小结
1:前言 做软件开发这几年遇到了许多的问题,也总结了一些问题的解决之道,之后慢慢的再遇到的都是一些重复性的问题了,当然,还有一些自己没有完全弄明白的问题.如果做的事情是重复的,遇到重复性问题的概率也就 ...
- 最长公共子序列LCS问题
很经典的一个问题,也是常考的问题
- 网页手机wap2.0网页的head里加入下面这条元标签......
网页手机wap2.0网页的head里加入下面这条元标签,在iPhone的浏览器中页面将以原始大小显示,并不允许缩放. <meta name="viewport" conten ...
- js 判断数组包含某值的方法 和 javascript数组扩展indexOf()方法
var questionId = []; var anSwerIdValue = []; ////javascript数组扩展indexOf()方法 Array.prototype.indexOf ...
- ajax通讯之格式详解
前言: ajax的出现,一定程度上改变了js的命运,同时也被广泛使用,而jq的兴起也大大降低了ajax的使用难度.虽然,jq的ajax方法使用起来十分便利,但是大部分开发人员也仅仅只是对其中的几个属性 ...
- git和nginx安装
原始地址: https://www.zybuluo.com/freeethy/note/192109 git安装 设置git的username和email (注册gitlab的账号密码) $$ git ...
- 23. Can't connect to X11 window server using '127.0.0.1:0.0' as the value of the DISPLAY variable.解决办法
在终端里 以root用户执行 #xhost + 然后su - oracle 执行#export DISPLAY=:0 运行runinstaller