/********************************************************************
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 测试程序的更多相关文章

  1. Spring+SpringMvc+Mybatis框架集成搭建教程三(框架整合测试程序开发)

    框架整合测试程序开发 (1).在mysql数据库中创建t_user表,sql语句如下 CREATE TABLE `t_user` ( `id` bigint(20) NOT NULL AUTO_INC ...

  2. Junit初级编码(一)第一个Junit测试程序

    序,Junit测试是单元测试的一个框架,提供了很多方法,供我们快速开展单元测试.目前最新版本JAR包为4.12,官网地址为http://junit.org/ 一.第一个Junit测试程序 1 去官网下 ...

  3. 【iCore3 双核心板】DEMO 1.0 测试程序发布

    iCore3 Demo V1.0 程序说明 一.概要 本资料包包含5个文件夹: 1.“arm”里是 icore3上 arm的程序包,开发环境为 KEIL 5.17: 2.“fpga”里是 icore3 ...

  4. float数据在内存中是怎么存储的 AND IEEE754测试程序

    float类型数字在计算机中用4个字节存储.遵循IEEE-754格式标准: 一个浮点数有2部分组成:底数m和指数e 底数部分 使用二进制数来表示此浮点数的实际值指数部分 占用8bit的二进制数,可表示 ...

  5. 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 ...

  6. STL 源码分析《2》----nth_element() 使用与源码分析

    Select 问题: 在一个无序的数组中 找到第 n 大的元素. 思路 1: 排序,O(NlgN) 思路 2: 利用快排的 RandomizedPartition(), 平均复杂度是 O(N) 思路 ...

  7. php测试程序运行时间和占用内存情况

    php测试程序运行时间和占用内存情况: $HeaderTime = microtime(true);//参数true表示返回浮点数值 /** *CODE */ printf(" total ...

  8. C# 测试程序运行时间和cpu使用时间

    方法一 Stopwatch类测试程序运行时间和cpu使用时间 添加命名空间using System.Diagnostics;使用实例如下 private Stopwatch sw = new Stop ...

  9. 学习实践:使用模式,原则实现一个C++自动化测试程序

    个人编程中比较喜欢重构,重构能够提高自己的代码质量,使代码阅读起来也更清晰.但是重构有一个问题,就是如何保证重构后带代码实现的功能与重构前的一致,如果每次重构完成后,对此不闻不问,则会有极大的风险,如 ...

随机推荐

  1. ADO数据库操作

    void CSjtestDlg::OnBnClickedButtonAdd() { // TODO: 在此添加控件通知处理程序代码 this->ShowWindow(SW_HIDE); DigA ...

  2. jQuery String Functions

    In today's post, I have put together all jQuery String Functions. Well, I should say that these are ...

  3. CRLF和LF

    协作项目,开发环境不同(mac,window)构建过程中,命令行报错(expecting LF but only find CRLF) 打开git bash,输入 $ git config --glo ...

  4. linux svn 提交文件时强制填写备注

    很多程序员不爱写注释,特别是svn提交的时候,文件做了什么修改都没有一个简单的备注,往往都是直接提交,这样是非常不利于团队开发的.所以就有了svn提交的时候,强制修改文件的备注. 步骤如下: 1.先找 ...

  5. 慕课网-Java入门第一季-7-3 Java 中无参带返回值方法的使用

    来源:http://www.imooc.com/code/1579 如果方法不包含参数,但有返回值,我们称为无参带返回值的方法. 例如:下面的代码,定义了一个方法名为 calSum ,无参数,但返回值 ...

  6. 浅谈oracle10G spfile与pfile(转)

    转自:http://blog.csdn.net/onebigday/article/details/6108348,http://www.linuxidc.com/Linux/2012-11/7371 ...

  7. django--app(六)

    1.应用加载 服务启动后,再新添加应用,是不会自动加载的,需要重启服务.

  8. [转载]python操作excel使用win32com

    原文链接:http://blog.163.com/yang_jianli/blog/static/16199000620138532243782/ 使用COM接口,直接操作EXCEL(只能在Win上) ...

  9. [转]逻辑斯蒂回归 via python

    # -*- coding:UTF-8 -*-import numpydef loadDataSet(): return dataMat,labelMat def sigmoid(inX): retur ...

  10. WeedFS依赖库 0.6.1

    WeedFS依赖库 版本 0.6.1 =======================================================================glog====== ...