简述:排序算法,参见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之排序算法--待解决的更多相关文章

  1. [Data Structure & Algorithm] 八大排序算法

    排序有内部排序和外部排序之分,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大,一次不能容纳全部的排序记录,在排序过程中需要访问外存.我们这里说的八大排序算法均为内部排序. 下图为排序 ...

  2. Java数据结构(七)—— 排序算法

    排序算法(Sort Algorithm) 排序算法介绍和分类 将一组数据,依指定顺序进行排列 排序的分类 内部排序 指将需要处理的所有数据都加载到内部存储器中进行排序 外部排序 数据量过大,无法全部加 ...

  3. 普林斯顿大学算法课 Algorithm Part I Week 3 排序算法复杂度 Sorting Complexity

    计算复杂度(Computational complexity):用于研究解决特定问题X的算法效率的框架 计算模型(Model of computation):可允许的操作(Allowable oper ...

  4. 谷歌的网页排序算法(PageRank Algorithm)

    本文将介绍谷歌的网页排序算法(PageRank Algorithm),以及它如何从250亿份网页中捞到与你的搜索条件匹配的结果.它的匹配效果如此之好,以至于“谷歌”(google)今天已经成为一个被广 ...

  5. 数据结构与算法---排序算法(Sort Algorithm)

    排序算法的介绍 排序也称排序算法 (Sort Algorithm),排序是将一组数据,依指定的顺序进行排列的过程. 排序的分类 1) 内部排序: 指将需要处理的所有数据都加载 到内部存储器(内存)中进 ...

  6. <Data Structure and Algorithm>排序算法

    排序稳定:如果两个数相同,对他们进行的排序结果为他们的相对顺序不变.例如A={1,2,1,2,1}这里排序之后是A = {1,1,1,2,2} 稳定就是排序后第一个1就是排序前的第一个1,第二个1就是 ...

  7. Algorithm --> 十大排序算法

    十大排序算法 主要排序法有:  一.冒泡( Bubble)排序—— 相邻交换  二.选择排序 ——每次最小/ 大排在相应的位置  三.插入排序 ——将下一个插入已排好的序列中  四.壳( Shell) ...

  8. [Algorithm]Algorithm章1 排序算法

    1.冒泡排序-相邻交换 (1)算法描述 冒泡排序是一种简单的排序算法.它重复地走访过要排序的数列,一次比较两个元素,如果它们的顺序错误就把它们交换过来.走访数列的工作是重复地进行直到没有再需要交换,也 ...

  9. 模板化的七种排序算法,适用于T* vector<T>以及list<T>

    最近在写一些数据结构以及算法相关的代码,比如常用排序算法以及具有启发能力的智能算法.为了能够让写下的代码下次还能够被复用,直接将代码编写成类模板成员函数的方式,之所以没有将这种方式改成更方便的函数模板 ...

随机推荐

  1. PHP中Soap模块安装与使用例子

    PHP5中的这个SOAP扩展目的是为了实现PHP对Web services的支持.与其它实现PHP对Web services的支持的方法不同,SOAP扩展是用C写的,因此它比其它方法具有速度优势 SO ...

  2. TCP可靠传输详解

    TCP提供了可靠的传输服务,这是通过下列方式提供的: 分块发送:应用数据被分割成TCP认为最适合发送的数据块.由TCP传递给IP的信息单位称为报文段或段(segment) 定时确认重传:当TCP发出一 ...

  3. python学习 01 变量

    1.变量不是‘盒子’. 1.1 不同的值,变量名没变,   变量地址也会变. 1.2 相同的值,不同的变量名,变量地址是相同的

  4. android 小游戏之数字猜猜

    http://www.cnblogs.com/whatbeg/p/4152333.html

  5. unity shader 编辑器扩展类 ShaderGUI

    这应该unity5才出的新功能了,今天看文档时刚巧看到了,就来尝试了一下. 效果如图: shader 的编辑器扩展分为2种方法: 是通过UnityEditor下的ShaderGUI类来实现的,形式比较 ...

  6. PHP-Manual的学习----【语言参考】----【基本语法】

    2017年6月28日11:29:311.当解析一个文件时,PHP 会寻找起始和结束标记,也就是 <?php 和 ?>,这告诉 PHP 开始和停止解析二者之间的代码.此种解析方式使得 PHP ...

  7. 安卓编译出错: Process 'command 'C:\Java\jdk1.8.0_51\bin\java.exe'' finished with non-zero exit value 1 解决!

    安卓编译出错: Process 'command 'C:\Java\jdk1.8.0_51\bin\java.exe'' finished with non-zero exit value 1 解决! ...

  8. 二、Android应用的界面编程(一)界面编程与视图(View)组件

    Android应用的绝大部分UI组件都放在android.widget包及其子包.android.view包及其子包中,Android应用的所有UI组件都继承了View类.它代表一个空白的矩形区域.V ...

  9. 2218 补丁vs错误

    2218 补丁vs错误 1999年CTSC国家队选拔赛  时间限制: 1 s  空间限制: 64000 KB  题目等级 : 大师 Master 题解       题目描述 Description 错 ...

  10. vs05字节对齐问题又一不小心就弄去了我一个下午的时间

    由于一字节的对齐问题,我调一个库调了我基本一个下午..... 犯错其实并不可怕, 可怕的是你一犯再犯...... 这也算得上是难能可贵... /Zp (Struct Member Alignment) ...