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>
最近在写一些数据结构以及算法相关的代码,比如常用排序算法以及具有启发能力的智能算法.为了能够让写下的代码下次还能够被复用,直接将代码编写成类模板成员函数的方式,之所以没有将这种方式改成更方便的函数模板 ...
随机推荐
- php判断页面是否是微信打开的示例(微信打开网页)
代码如下: $user_agent = $_SERVER['HTTP_USER_AGENT'];if (strpos($user_agent, 'MicroMessenger') === false) ...
- k8s部署nginx集群
环境: 两台虚拟机, 10.10.20.203 部署docker.etcd.flannel.kube-apiserver.kube-controller-manager.kube-scheduler ...
- Zabbix二次开发_03api列表
基于zabbix 3.0 https://www.zabbix.com/documentation/3.0/manual/api/reference Method reference This sec ...
- windows上mysql安装
1. 下载MySQL Community Server 5.7.14 Index of /MySQL/Downloads/MySQL-Cluster-7.1 2. 解压MySQL压缩包 安装路径:E: ...
- 解决ubuntu无法进入unity模式
终端输入如下命令: 1.sudo add-apt-repository ppa:gnome3-team/gnome3 2.sudo apt-get update 3.sudo apt-get inst ...
- SDOI2012 Round1 day2 拯救小云公主(dis)解题报告
#include<cstdio> #include<cmath> #include<iostream> using namespace std; typedef l ...
- 解决PL/SQL Developer过期
1 2 3 4 5 6 分步阅读 PL/SQL Developer过期了,又没有注册码,又不想花钱买,而且事情又非常急,这时候怎么办?不要着急,请随小编一起解决这种情况吧. 工具/原料 PL/SQ ...
- nginx学习之压缩解压篇(七)
1.简介 压缩响应可以减少传输数据的大小,节省带宽.但过多的压缩会造成很大的处理开销.在发送给客户端之前,nginx会对响应做压缩,但是如果后端服务器已经 压缩过了,nginx就不再压缩. 2.开启压 ...
- PAT 1063. 计算谱半径(20)
在数学中,矩阵的“谱半径”是指其特征值的模集合的上确界.换言之,对于给定的n个复数空间的特征值{a1+b1i, ..., an+bni},它们的模为实部与虚部的平方和的开方,而“谱半径”就是最大模. ...
- 忘记apple id如何更新应用?
最近ytkah的app有很多更新提示,之前注册的apple id好久没登录了,突然提示说登录需要验证安全问题,哪还记得噢,最要命的是邮箱收到的加密邮件也需要验证.重新注册一个吧,这次要注意保存相关信息 ...