python的sorted()使用什么算法? [英] What algorithm does python's sorted() use?

查看:324
本文介绍了python的sorted()使用什么算法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
关于python的内置sort()方法

Possible Duplicate:
About python's built in sort() method

名字说明了一切.

我正试图向某人解释为什么他们应该使用Python的内置sorted()函数而不是滚动自己的函数,我意识到我不知道它使用什么算法.

I'm trying to explain to someone why they should use Python's builtin sorted() function instead of rolling their own, and I realized I have no idea what algorithm it uses.

如果有关系,我们正在谈论python 2.7

If it matters, we're talking python 2.7

推荐答案

Python使用名为 Timsort :

Python uses an algorithm called Timsort:

Timsort是一种混合排序算法,源自合并排序和 插入排序,旨在在多种现实世界中表现出色 数据.它是蒂姆·彼得斯(Tim Peters)在2002年发明的,用于Python 编程语言.该算法查找以下数据的子集 已经排序,并使用子集对数据进行更多排序 有效率的.这是通过合并一个已识别的子集(称为 运行,直到满足某些条件为止. Timsort 自2.3版以来,一直是Python的标准排序算法.它是 现在还用于在Java SE 7和Android上对数组进行排序 平台.

Timsort is a hybrid sorting algorithm, derived from merge sort and insertion sort, designed to perform well on many kinds of real-world data. It was invented by Tim Peters in 2002 for use in the Python programming language. The algorithm finds subsets of the data that are already ordered, and uses the subsets to sort the data more efficiently. This is done by merging an identified subset, called a run, with existing runs until certain criteria are fulfilled. Timsort has been Python's standard sorting algorithm since version 2.3. It is now also used to sort arrays in Java SE 7, and on the Android platform.

这篇关于python的sorted()使用什么算法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆