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[] = {32,71,12,45,26,80,53,33};
    std::vector<int> myvector(myints, myints+8);            // 32 71 12 45 26 80 53 33

    std::sort(myvector.begin(), myvector.begin()+4);        //(12 32 45 71)26 80 53 33

    std::sort(myvector.begin()+4, 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 0;
}
/*
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+8);

    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+8);

    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 0;
}
/*
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[] = {4, 10, 70, 30, 10, 69, 96, 7};
    std::vector<int> myvector(myints, myints+8);

    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()+4, 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 0;}
/*
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=1; i<10; 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()+5, 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 0;
}

//注:实际上输出的是一个全排序的结果,这是因为该算法设置了一个优化,当区间元素个数不大于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[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    std::random_shuffle(myints, myints+9);

    std::vector<int> myvector(5);

    std::partial_sort_copy(myints, myints+9, 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 0;
}
时间: 2025-01-06 15:51:18

algorithm之排序算法--待解决的相关文章

[Data Structure &amp; Algorithm] 八大排序算法

排序有内部排序和外部排序之分,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大,一次不能容纳全部的排序记录,在排序过程中需要访问外存.我们这里说的八大排序算法均为内部排序. 下图为排序算法体系结构图: 1. 直接插入排序(Straight Insertion Sort ) 基本思想:将待排序的无序数列看成是一个仅含有一个元素的有序数列和一个无序数列,将无序数列中的元素逐次插入到有序数列中,从而获得最终的有序数列. 算法流程: 1)初始时, a[0]自成一个有序区, 无序区为a[1

【数据结构】选择排序算法示例

基本选择排序编辑 排序算法即解决以下问题的算法: 输入 n个数的序列<a1,a2,a3,...,an>. 输出 原序列的一个重排<a1*,a2*,a3*,...,an*>:,使得a1*<=a2*<=a3*<=...<=an* 排序算法有很多,包括插入排序,冒泡排序,堆排序,归并排序,选择排序,计数排序,基数排序,桶排序,快速排序等.插入排序,堆排序,选择排序,归并排序和快速排序,冒泡排序都是比较排序,它们通过对数组中的元素进行比较来实现排序,其他排序算法则是

c++STL之常用排序算法

sort:对容器元素进行排序 random_shuffle:洗牌,指定范围内的元素随机调整次序 merge:容器元素合并,并存储到另一容器中 reverse:反转指定范围内的元素 1.sort #include<iostream> using namespace std; #include <algorithm> #include <vector> #include <functional> //常用排序算法 sort void myPrint(int va

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

计算复杂度(Computational complexity):用于研究解决特定问题X的算法效率的框架 计算模型(Model of computation):可允许的操作(Allowable operations) 成本模型(Cost model):操作数(Operation counts) 上界(Upper bound):最多的成本 下界(Lower bound):最少的成本 最优算法(Optimal algorithm):最有可能性的成本的算法(Algorithm with best pos

Collection of algorithm for sorting. 常见排序算法集(一)

Collection of algorithm for sorting (part one) 选择排序--Selection sort 可能你会觉得奇怪,为什么用结构体传参? 那是一层封装,我在面向对象的思想来写 : ) 对已一个数组,不管怎么操作,都不能越界. C语言 不会自动检查数组越界.这个时候程序员就应该负起责任来. 数组的边界成为数组必不可少的信息,此时可以把array当作一个对象来看,即此时的element. 我们排序的对象可能会变,比方说从int类型变到char类型,这时候我们仅仅

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

排序算法的介绍 排序也称排序算法 (Sort Algorithm),排序是将一组数据,依指定的顺序进行排列的过程. 排序的分类 1) 内部排序: 指将需要处理的所有数据都加载 到内部存储器(内存)中进行排序. 2) 外部排序法:数据量过大,无法全部加载到内 存中,需要借助外部存储(文件等)进行 排序. 常见的排序算法分类 算法的时间复杂度 度量一个程序(算法)执行时间的两种方法 1.事后统计的方法这种方法可行, 但是有两个问题:一是要想对设计的算法的运行性能进行评测,需要实际运行该程序: 二是所

7 种排序算法/sort algorithm

常见的排序算法_百度搜索https://www.baidu.com/s?wd=%E5%B8%B8%E8%A7%81%E7%9A%84%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95&rsv_spt=1&rsv_iqid=0xdefec6740000c566&issp=1&f=8&rsv_bp=0&rsv_idx=2&ie=utf-8&tn=site888_3_pg&rsv_enter=1&rsv_sug

模板化的七种排序算法,适用于T* vector&lt;T&gt;以及list&lt;T&gt;

最近在写一些数据结构以及算法相关的代码,比如常用排序算法以及具有启发能力的智能算法.为了能够让写下的代码下次还能够被复用,直接将代码编写成类模板成员函数的方式,之所以没有将这种方式改成更方便的函数模板纯属于偷懒,更方便于测试代码的有效性,等代码写完也懒得去改了.下面开始介绍这段代码,有什么不对的地方欢迎前来指正. 一共写了七种排序,插入排序InsertSort.堆排序HeapSort.快速排序QuickSort.合并排序MergeSort,计数排序CountingSort,基数排序RadixSo

【Unity3D自学记录】可视化对照十多种排序算法(C#版)

在这篇文章中.我会向大家展示一些排序算法的可视化过程.我还写了一个工具.大家可对照查看某两种排序算法. 下载源代码 – 75.7 KB 下载演示样例 – 27.1 KB 引言 首先,我觉得是最重要的是要理解什么是"排序算法".依据维基百科.排序算法(Sorting algorithm)是一种能将一串数据按照特定排序方式进行排列的一种算法. 最经常使用到的排序方式是数值顺序以及字典顺序.有效的排序算法在一些算法(比如搜索算法与合并算法)中是重要的,如此这些算法才干得到正确解答.排序算法也