当前位置:网站首页>归并排序最简单的写法——只需要不到15行
归并排序最简单的写法——只需要不到15行
2022-06-10 08:05:00 【__JAN__】
众所周知,归并排序是一个稳定的,快速的排序算法,实现起来也比较容易,今天重温归并的时候发现了配合stl算法竟然可以写出一个这么短的归并排序。
当然,这个版本不具很好备泛型能力。
template <typename T>
void merge_sort(T arr[],int left,int right)
{
if(left < right)
{
int mid = (left + right) / 2;
merge_sort(arr, left, mid);
merge_sort(arr, mid+1, right);
vector<T> vec(right-left+1);
merge(arr+left,arr+mid+1,arr+mid+1,arr+right+1,vec.begin());
copy(vec.begin(),vec.end(),arr+left);
}
}这样写或许不够C++,是C加上模板,感觉有点不伦不类。下面这个改写的版本,虽然笔者没有写双向迭代器的版本,但是也具有很好的泛型,具备标准库似的泛型能力。能和STl很好的兼容,并且前闭后开的写法也使得这样更加容易阅读。
template <typename RandomIter>
void merge_sort_iter(RandomIter first, RandomIter last)
{
if(first == last - 1)
return;
using distance_type = typename iterator_traits<RandomIter>::difference_type;
distance_type dis = (last - first) / 2;
auto mid = first + dis;
merge_sort_iter(first,mid);
merge_sort_iter(mid, last);
using value_type = typename iterator_traits<RandomIter>::value_type;
vector<value_type> vec(last-first);
merge(first,mid,mid,last,vec.begin());
copy(vec.begin(),vec.end(),first);
}以后3分钟手搓一个差强人意的排序不是梦了。
ps: std::sort: 你说啥?
边栏推荐
- How to make sql-mode=“NO_ENGINE_SUBSTITUTION” permanent in MySQL my. cnf
- qt制作简易的视频通话
- OS实验七【文件管理】
- Introduction to temporal database incluxdb
- 3 ZK's election mechanism
- 618 big promotion is in progress, Cloud Computing supports e-commerce live broadcast
- Leetcode 160 Intersecting linked list (2022.06.09)
- Iterator迭代器,while循环,增强for循环的用法
- SQL makes a column empty
- 下一代企业IT架构:云原生架构
猜你喜欢

PyQt5基础学习

Iterator iterator, while loop, enhanced for loop usage

putty里中文显示为框框和乱码无法显示中文解决

YOLO-SLAM: A semantic SLAM system towards dynamic environment with geometric constraint

PS 2022 installation failure error code 182 solution

3 ZK's election mechanism

mysql之pt-kill

2. ZK's working mechanism

Open source polardb's overall structure design and enterprise level features for the first time

A practice of encrypting server 3D model resources
随机推荐
[untitled]
7-1 intersection of two ordered linked list sequences (20 points)
Next generation enterprise IT architecture: cloud native architecture
【Flutter 问题系列第 65 篇】在 Flutter 设置 showModalBottomSheet 最大高度无效的解决方案
Notice on the issuance of Shenzhen action plan for cultivating and developing semiconductor and integrated circuit industry clusters (2022-2025)
【软件测试】多家大厂的软件测试面试常见问题合集(BAT、三大流量厂商、知名大厂)
Global industry analysis report of Internet subtitle service in 2022
Vulnerability recurrence_ Cve-2020-0796 eternal black vulnerability_ Pit encounter_ resolved
Resistance, capacitance, inductance
OS实验六【设备管理】
Global industry analysis report of proton therapy technology in 2022
Common MySQL commands for viewing database information
String problem summary
[dry goods] typescript: extensions, infer and DVA type
PT kill of MySQL
Notice on the issuance of Shenzhen action plan for cultivating and developing software and information service industry clusters (2022-2025)
工程上对排序的改进
Internet comics 2022 Global Industry Analysis Report
Using pyqt5 + Agora + leancloud to realize online class based on student fatigue detection
Global industry analysis report of food dehydrators in 2022