algorithm之排序算法--待解决
简述:排序算法,参见http://www.cplusplus.com/reference/algorithm/?kw=algorithm
待解决问题:各种排序算法的实现
/*
template <class RandomAccessIterator>
void sort (RandomAccessIterator first, RandomAccessIterator last); template <class RandomAccessIterator, class Compare>
void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp); Sort elements in range
Sorts the elements in the range [first,last) into ascending order.
[将[first, last)中的元素升序排列]
The elements are compared using operator< for the first version, and comp for the second.
[元素的比较是通过operator<进行的(或者comp)]
Equivalent elements are not guaranteed to keep their original relative order (see stable_sort).
[相等元素的原有排序不会被保证(如果要保证原有排序,参见stable_sort)]
*/ #include <iostream>
#include <algorithm>
#include <vector> bool myfunction (int i, int j){
return (i<j);
} class myclass
{
public:
bool operator() (int i, int j){
return (i<j);
}
}; int main()
{
int myints[] = {,,,,,,,};
std::vector<int> myvector(myints, myints+); // 32 71 12 45 26 80 53 33 std::sort(myvector.begin(), myvector.begin()+); //(12 32 45 71)26 80 53 33 std::sort(myvector.begin()+, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80) std::sort(myvector.begin(), myvector.end(), myclass()); //(12 26 32 33 45 53 71 80) std::cout<<"myvector contains:";
for(std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it)
std::cout<<' '<<*it;
std::cout<<'\n'; system("pause");
return ;
}
/*
template <class RandomAccessIterator>
void stable_sort (RandomAccessIterator first, RandomAccessIterator last); template <class RandomAccessIterator, class Compare>
void stable_sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp); Sort elements preserving order of equivalents
Sorts the elements in the range [first,last) into ascending order, like sort, but stable_sort preserves the relative order of the elements with equivalent values.
[将[first, last)中的元素升序排列,不同与sort(),该算法会保留相等元素的相对排序]
The elements are compared using operator< for the first version, and comp for the second.
[元素的比较通过operator<来进行(或者通过comp)]
*/ #include <iostream>
#include <algorithm>
#include <vector> bool compare_as_ints (double i, double j){
return (int(i)<int(j));
} int main()
{
double mydoubles[] = {3.14, 1.41, 2.72, 4.67, 1.73, 1.32, 1.62, 2.58}; std::vector<double> myvector; myvector.assign(mydoubles, mydoubles+); std::cout<<"using default comparison:";
std::stable_sort(myvector.begin(), myvector.end());
for(std::vector<double>::iterator it = myvector.begin(); it != myvector.end(); ++it)
std::cout<<' '<<*it;
std::cout<<'\n'; myvector.assign(mydoubles, mydoubles+); std::cout<<"using custom comparison:";
std::stable_sort(myvector.begin(), myvector.end(), compare_as_ints);
for(std::vector<double>::iterator it = myvector.begin(); it != myvector.end(); ++it)
std::cout<<' '<<*it;
std::cout<<'\n'; system("pause");
return ;
}
/*
template <class RandomAccessIterator>
void partial_sort (RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last); template <class RandomAccessIterator, class Compare>
void partial_sort (RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Compare comp); Partially sort elements in range
[局部排序]
Rearranges the elements in the range [first,last), in such a way that the elements before middle are the smallest elements in the entire range and are sorted in ascending order, while the remaining elements are left without any specific order.
[将最小的一些元素按升序排列在[first, middle)中,剩下的未指定排序的元素放在[middle, last)中]
The elements are compared using operator< for the first version, and comp for the second.
[元素的比较通过operator<进行(或者通过comp)]
*/ #include <iostream>
#include <algorithm>
#include <vector> bool myfunction (int i, int j){
return (i<j);
} int main()
{
int myints[] = {, , , , , , , };
std::vector<int> myvector(myints, myints+); std::vector<int>::iterator it; std::cout<<"Before calling partial_sort:";
for(it = myvector.begin(); it != myvector.end(); ++it)
std::cout<<' '<<*it;
std::cout<<'\n'; std::partial_sort(myvector.begin(), myvector.begin()+, myvector.end(), myfunction); std::cout<<"Before calling partial_sort:";
for(it = myvector.begin(); it != myvector.end(); ++it)
std::cout<<' '<<*it;
std::cout<<'\n'; system("pause");
return ;
}
/*
template <class RandomAccessIterator>
void nth_element (RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last); template <class RandomAccessIterator, class Compare>
void nth_element (RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last, Compare comp); Sort element in range
Rearranges the elements in the range [first,last), in such a way that the element at the nth position is the element that would be in that position in a sorted sequence.
[将[first, last)中第nth小的元素排列在第nth位置上]
The other elements are left without any specific order, except that none of the elements preceding nth are greater than it, and none of the elements following it are less.
[至于其他元素则不会指定排序,但要使所有小于第nth个元素的元素都排在它前面,而大于它的元素都排在后面]
The elements are compared using operator< for the first version, and comp for the second.
[元素的比较是通过operator<进行的(或者comp)]
*/ #include <iostream> // std::cout
#include <algorithm> // std::nth_element, std::random_shuffle
#include <vector> // std::vector bool myfunction (int i,int j) { return (i<j); } int main ()
{
std::vector<int> myvector; // set some values:
for (int i=; i<; i++) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9 std::random_shuffle (myvector.begin(), myvector.end()); std::nth_element (myvector.begin(), myvector.begin()+, myvector.end(),myfunction); std::cout << "myvector contains:";
for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n'; system("pause");
return ;
} //注:实际上输出的是一个全排序的结果,这是因为该算法设置了一个优化,当区间元素个数不大于32时,直接做一次全排序,只有当元素个数超过32时,才采用局部排序算法
/*
template <class InputIterator, class RandomAccessIterator>
RandomAccessIterator partial_sort_copy (InputIterator first, InputIterator last, RandomAccessIterator result_first, RandomAccessIterator result_last); template <class InputIterator, class RandomAccessIterator, class Compare>
RandomAccessIterator partial_sort_copy (InputIterator first, InputIterator last, RandomAccessIterator result_first, RandomAccessIterator result_last, Compare comp); Copy and partially sort range
Copies the smallest elements in the range [first,last) to [result_first,result_last), sorting the elements copied. The number of elements copied is the same as the distance between result_first and result_last (unless this is more than the amount of elements in [first,last)).
[将[first, last)中最小的一些元素复制到[result_first, result_last)中并排序,被复制的元素个数为result_last-result_first个]
The range [first,last) is not modified.
[区间[first, last)中的元素没有被改变]
The elements are compared using operator< for the first version, and comp for the second.
[元素的比较通过operator<来进行(或者通过comp)]
*/ #include <iostream>
#include <algorithm>
#include <vector> int myfunction(int i, int j){
return i<j;
} int main()
{
int myints[] = {, , , , , , , , };
std::random_shuffle(myints, myints+); std::vector<int> myvector(); std::partial_sort_copy(myints, myints+, myvector.begin(), myvector.end(), myfunction); std::cout<<"myvector contains:";
for(std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it)
std::cout<<' '<<*it;
std::cout<<'\n'; system("pause");
return ;
}
algorithm之排序算法--待解决的更多相关文章
- [Data Structure & Algorithm] 八大排序算法
排序有内部排序和外部排序之分,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大,一次不能容纳全部的排序记录,在排序过程中需要访问外存.我们这里说的八大排序算法均为内部排序. 下图为排序 ...
- Java数据结构(七)—— 排序算法
排序算法(Sort Algorithm) 排序算法介绍和分类 将一组数据,依指定顺序进行排列 排序的分类 内部排序 指将需要处理的所有数据都加载到内部存储器中进行排序 外部排序 数据量过大,无法全部加 ...
- 普林斯顿大学算法课 Algorithm Part I Week 3 排序算法复杂度 Sorting Complexity
计算复杂度(Computational complexity):用于研究解决特定问题X的算法效率的框架 计算模型(Model of computation):可允许的操作(Allowable oper ...
- 谷歌的网页排序算法(PageRank Algorithm)
本文将介绍谷歌的网页排序算法(PageRank Algorithm),以及它如何从250亿份网页中捞到与你的搜索条件匹配的结果.它的匹配效果如此之好,以至于“谷歌”(google)今天已经成为一个被广 ...
- 数据结构与算法---排序算法(Sort Algorithm)
排序算法的介绍 排序也称排序算法 (Sort Algorithm),排序是将一组数据,依指定的顺序进行排列的过程. 排序的分类 1) 内部排序: 指将需要处理的所有数据都加载 到内部存储器(内存)中进 ...
- <Data Structure and Algorithm>排序算法
排序稳定:如果两个数相同,对他们进行的排序结果为他们的相对顺序不变.例如A={1,2,1,2,1}这里排序之后是A = {1,1,1,2,2} 稳定就是排序后第一个1就是排序前的第一个1,第二个1就是 ...
- Algorithm --> 十大排序算法
十大排序算法 主要排序法有: 一.冒泡( Bubble)排序—— 相邻交换 二.选择排序 ——每次最小/ 大排在相应的位置 三.插入排序 ——将下一个插入已排好的序列中 四.壳( Shell) ...
- [Algorithm]Algorithm章1 排序算法
1.冒泡排序-相邻交换 (1)算法描述 冒泡排序是一种简单的排序算法.它重复地走访过要排序的数列,一次比较两个元素,如果它们的顺序错误就把它们交换过来.走访数列的工作是重复地进行直到没有再需要交换,也 ...
- 模板化的七种排序算法,适用于T* vector<T>以及list<T>
最近在写一些数据结构以及算法相关的代码,比如常用排序算法以及具有启发能力的智能算法.为了能够让写下的代码下次还能够被复用,直接将代码编写成类模板成员函数的方式,之所以没有将这种方式改成更方便的函数模板 ...
随机推荐
- 2018,从AI看安卓生态的变革
AI的发展与影响 与传统技术不同的是,AI技术算法清晰,优化目标明确,基础技术成熟,使得一众中小创企也看到了市场的机会.2017年中国企业动作频频,在自动驾驶,智能安防,智慧城市等领域都取得了不俗的成 ...
- wps文档怎样去除广告
安装完 WPS 之后右键—属性—打开文件夹位置(如图) 接下来进入 10.1.0.6929 文件夹内,再次进入 office6 文件夹内,找到 wpscenter 应用程序,将其删除.此应用包含定时弹 ...
- Python修饰器的函数式编程(转)
From:http://coolshell.cn/articles/11265.html 作者:陈皓 Python的修饰器的英文名叫Decorator,当你看到这个英文名的时候,你可能会把其跟Desi ...
- 程序包 javax.servlet 不存在 解决办法
其原因是java编译器没有找到软件包javax.servlet. 下载servlet.jar放到lib下没有效果,后发现需要在jdk中添加,如下: 解决办法: 从tomcat lib目录下拷贝一个se ...
- python pytest
之前一直用unittest ,现在学习pytest 看看那个好 1. 安装 pip install -U pytest py.test --version 2. 只需要按照下面的规则: 测试文件以te ...
- DFS应用——查找强分支
[0]README 0.1) 本文总结于 数据结构与算法分析, 源代码均为原创, 旨在 理解 "DFS应用--查找强分支" 的idea 并用源代码加以实现 : [1]查找强分支 1 ...
- atom常用插件安装
安装插件方法: File -Settings -Install 在搜索框里搜索你想要的插件,出来之后 点击install ,下图以 linter-selint 为例 ATOM常用插件推荐 simpli ...
- Linux进程间通信(三) - 信号
什么是信号 软中断信号(signal,又简称为信号)用来通知进程发生了异步事件.在软件层次上是对中断机制的一种模拟,在原理上,一个进程收到一个信号与处理器收到一个中断请求可以说是一样的.信号是进程间通 ...
- gridControl使用集锦
1.grid控件默认选择一行时,focused的cell并不是蓝色的,而是白色的 要想实现一次选择一行全都是蓝色的只要改一个属性就可以了 this.gridView1.OptionsSelection ...
- FineUIPro中如何支持多语言(全局资源文件和本地资源文件)
一个客户在邮件中问到了FineUIPro的多语言实现问题,其实 FineUIPro 并没有对此做特殊处理,因此直接使用 ASP.NET 原生支持的资源文件就能实现. 下面我们就以FineUIPro的空 ...