STL算法(Algorithms):排序

本文深入解析了C++中几种常用的排序算法:sort、stable_sort、partial_sort与nth_element,包括它们的原理、用法及示例代码。详细解释了如何使用这些算法进行不同场景下的数据排序。

一、sort:对一定范围内的所有元素排序

原型:

template <class RandomAccessIterator>

void sort ( RandomAccessIterator first, RandomAccessIterator last );

template <class RandomAccessIterator, class Compare>

void sort ( RandomAccessIterator first, RandomAccessIterator last, Compare comp );
例子代码:

// sort algorithm example

#include <iostream>

#include <algorithm>

#include <vector>

using namespace std;

bool myfunction (int i,int j) { return (i<j); }

struct myclass {

bool operator() (int i,int j) { return (i<j);}

} myobject;

int main () {

int myints[] = {32,71,12,45,26,80,53,33};

vector<int> myvector (myints, myints+8);// 32 71 12 45 26 80 53 33

vector<int>::iterator it;

// using default comparison (operator <):

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

// using function as comp

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

// using object as comp

sort (myvector.begin(), myvector.end(), myobject);//(12 26 32 33 45 53 71 80)

// print out content:

cout << "myvector contains:";

for (it=myvector.begin(); it!=myvector.end(); ++it)

cout << " " << *it;

cout << endl;

return 0;

}

二、stable_sort:归并排序

原型:

template <class RandomAccessIterator>

void stable_sort ( RandomAccessIterator first, RandomAccessIterator last );

template <class RandomAccessIterator, class Compare>

void stable_sort ( RandomAccessIterator first, RandomAccessIterator last,

Compare comp );
例子代码:

// stable_sort example

#include <iostream>

#include <algorithm>

#include <vector>

using namespace std;

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};

vector<double> myvector;

vector<double>::iterator it;

myvector.assign(mydoubles,mydoubles+8);

cout << "using default comparison:";

stable_sort (myvector.begin(), myvector.end());

for (it=myvector.begin(); it!=myvector.end(); ++it)

cout << " " << *it;

myvector.assign(mydoubles,mydoubles+8);

cout << "\nusing 'compare_as_ints' :";

stable_sort (myvector.begin(), myvector.end(), compare_as_ints);

for (it=myvector.begin(); it!=myvector.end(); ++it)

cout << " " << *it;

cout << endl;

return 0;

}

三、partial_sort_copy:对序列中的某个范围的元素进行排序(部分排序)

原型:

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 );
示例代码:

// partial_sort example
#include <iostream>
#include <algorithm>
#include <vector>
usingnamespacestd;

boolmyfunction (inti,intj) {return(i<j); }

intmain () {
intmyints[] = {9,8,7,6,5,4,3,2,1};
vector<int> myvector (myints, myints+9);
vector<int>::iterator it;

// using default comparison (operator <):
partial_sort (myvector.begin(), myvector.begin()+5, myvector.end());

// using function as comp
partial_sort (myvector.begin(), myvector.begin()+5, myvector.end(),myfunction);

// print out content:
cout <<"myvector contains:";
for(it=myvector.begin(); it!=myvector.end(); ++it)
cout <<" "<< *it;

cout << endl;

return0;
}

四、partial_sort_copy部分排序后拷贝
原型:

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

示例:

// partial_sort_copy example#include <iostream>#include <algorithm>#include <vector>using namespace std;bool myfunction (int i,int j) { return (i<j); }int main () {  int myints[] = {9,8,7,6,5,4,3,2,1};  vector<int> myvector (5);  vector<int>::iterator it;  // using default comparison (operator <):  partial_sort_copy (myints, myints+9, myvector.begin(), myvector.end());  // using function as comp  partial_sort_copy (myints, myints+9, myvector.begin(), myvector.end(), myfunction);  // print out content:  cout << "myvector contains:";  for (it=myvector.begin(); it!=myvector.end(); ++it)    cout << " " << *it;  cout << endl;  return 0;}
五、nth_element:使第n个元素处在序列中的第n个位置,并在这个元素前都是比这个元素小的,
这个元素后面都是比这个这元素大,但是这前后两个序列并不一定都是有序的(排过序);
原型: 

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

例子代码:
// nth_element example#include <iostream>#include <algorithm>#include <vector>using namespace std;bool myfunction (int i,int j) { return (i<j); }int main () {  vector<int> myvector;  vector<int>::iterator it;  // set some values:  for (int i=1; i<10; i++) myvector.push_back(i);   // 1 2 3 4 5 6 7 8 9  random_shuffle (myvector.begin(), myvector.end());  // using default comparison (operator <):  nth_element (myvector.begin(), myvector.begin()+5, myvector.end());  // using function as comp  nth_element (myvector.begin(), myvector.begin()+5, myvector.end(),myfunction);  // print out content:  cout << "myvector contains:";  for (it=myvector.begin(); it!=myvector.end(); ++it)    cout << " " << *it;  cout << endl;  return 0;}

分布式微服务企业级系统是一个基于Spring、SpringMVC、MyBatis和Dubbo等技术的分布式敏捷开发系统架构。该系统采用微服务架构和模块化设计,提供整套公共微服务模块,包括集中权限管理(支持单点登录)、内容管理、支付中心、用户管理(支持第三方登录)、微信平台、存储系统、配置中心、日志分析、任务和通知等功能。系统支持服务治理、监控和追踪,确保高可用性和可扩展性,适用于中小型企业的J2EE企业级开发解决方案。 该系统使用Java作为主要编程语言,结合Spring框架实现依赖注入和事务管理,SpringMVC处理Web请求,MyBatis进行数据持久化操作,Dubbo实现分布式服务调用。架构模式包括微服务架构、分布式系统架构和模块化架构,设计模式应用了单例模式、工厂模式和观察者模式,以提高代码复用性和系统稳定性。 应用场景广泛,可用于企业信息化管理、电子商务平台、社交应用开发等领域,帮助开发者快速构建高效、安全的分布式系统。本资源包含完整的源码和详细论文,适合计算机科学或软件工程专业的毕业设计参考,提供实践案例和技术文档,助力学生和开发者深入理解微服务架构和分布式系统实现。 【版权说明】源码来源于网络,遵循原项目开源协议。付费内容为本人原创论文,包含技术分析和实现思路。仅供学习交流使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值