用Python最快的排序方式 [英] Fastest way to sort in Python

查看:79
本文介绍了用Python最快的排序方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中对大于0且小于100000的整数数组进行排序的最快方法是什么?但不要使用诸如sort之类的内置函数.

What is the fastest way to sort an array of whole integers bigger than 0 and less than 100000 in Python? But not using the built in functions like sort.

我正在考虑根据输入大小组合两种运动功能的可能性.

Im looking at the possibility to combine 2 sport functions depending on input size.

推荐答案

如果您对渐近时间感兴趣,则对sort或radix排序进行计数会提供良好的性能.

If you are interested in asymptotic time, then counting sort or radix sort provide good performance.

但是,如果您对挂钟时间感兴趣,您将需要使用您的特定数据集比较不同算法之间的性能,因为不同算法对不同数据集的执行方式会有所不同.在这种情况下,总是值得尝试快速排序:

However, if you are interested in wall clock time you will need to compare performance between different algorithms using your particular data sets, as different algorithms perform differently with different datasets. In that case, its always worth trying quicksort:

def qsort(inlist):
    if inlist == []: 
        return []
    else:
        pivot = inlist[0]
        lesser = qsort([x for x in inlist[1:] if x < pivot])
        greater = qsort([x for x in inlist[1:] if x >= pivot])
        return lesser + [pivot] + greater

来源: http://rosettacode.org/wiki/Sorting_algorithms/Quicksort#Python

这篇关于用Python最快的排序方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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