std :: sort和std :: stable

编程入门 行业动态 更新时间:2024-10-11 19:20:55
本文介绍了std :: sort和std :: stable_sort有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想知道std :: sort和std :: stable_sort在功能,内存和硬件方面有何不同? 文档提到将[first,last)范围内的元素升序排列。顺序,例如排序,但stable_sort保留具有相等值的元素的相对顺序。,但这对我来说没有意义。什么是相对顺序和等效值?

I would like to know how std::sort and std::stable_sort differ with respect to functionality, memory and hardware? The documentation mentions that "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.", but that didn't make sense to me. What is the "relative order" and "equivalent values"?

推荐答案

是的,正如您所说,这不是

Yes, it's as you said, and this is not a concept unique to C++.

稳定排序保留了语义等效值的物理顺序。

std :: sort :

不能保证相等元素的顺序被保留。 复杂度:: O(N·log(N)),其中 N = std :: distance(第一个,最后一个)比较

std :: stable_sort :

保证相等元素的顺序得以保留。 复杂性: O(N· log ^ 2(N)),其中 N = std :: distance(first,last) cmp 的应用程序。如果有更多可用内存,则复杂度为 O(N·log(N))。

The order of equal elements is guaranteed to be preserved. Complexity: O(N·log^2(N)), where N = std::distance(first, last) applications of cmp. If additional memory is available, then the complexity is O(N·log(N)).

含义是 std :: stable_sort 在执行时间方面无法达到同样高效的效果,除非可用附加内存就内存消耗而言,执行效率不高。

The implication is that std::stable_sort cannot be performed quite as efficiently in terms of execution time, unless "additional memory is available" in which case it is not being performed as efficiently in terms of memory consumption.

更多推荐

std :: sort和std :: stable

本文发布于:2023-11-30 06:06:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648971.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:std   sort   stable

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!